jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not work after publish as single file

gemoglobin opened this issue · comments

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.TypeInitializationException: The type initializer for 'FlatSharp.RoslynSerializerGenerator' threw an exception.
---> System.ArgumentException: Empty path name is not legal. (Parameter 'path')
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.File.OpenRead(String path)
at Roslyn.Utilities.FileUtilities.OpenFileStream(String path)
at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation)
at FlatSharp.RoslynSerializerGenerator..cctor()
--- End of inner exception stack trace ---
at FlatSharp.RoslynSerializerGenerator..ctor(FlatBufferSerializerOptions options, TypeModelContainer typeModelContainer)
at FlatSharp.FlatBufferSerializer.GetOrCreateTypedSerializerTRoot
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at FlatSharp.FlatBufferSerializer.GetOrCreateUntypedSerializer(Type itemType)
at FlatSharp.FlatBufferSerializer.GetMaxSize[T](T item)
at Samples.MonsterAttributeExample.MonsterAttributeExample.Run()
at Samples.Program.Main(String[] args)

Can you share detailed instructions on how to trigger this exception?

  • What FBS file or C# definition did you use?
  • What OS/Framework are you using?
  • What SDKs does your computer have installed? (dotnet --list-sdks)
  • Any other details you think might be specific to your scenario.

Thanks!

1.MonsterAttributeExample
2. Win10 .NET 5

This doesn't reproduce on my machine with the same configuration as you (as far as I can tell). I'm happy to investigate further if you're willing to provide more information, but this is not actionable as written.

This doesn't reproduce on my machine with the same configuration as you (as far as I can tell). I'm happy to investigate further if you're willing to provide more information, but this is not actionable as written.

Sorry, now i can give more information. .NET 5 SDK 5.0.301, Visual Studio 16.10.2, have you really tried to publish with a single file option? Maybe problem is option <SelfContained>true</SelfContained>

Oh okay. I did not try <SelfContained>! I will investigate. Thanks for the additional detail!

I've applied <SelfContained> to Samples.csproj:

  <PropertyGroup>
    <TargetFrameworks>net5.0</TargetFrameworks>
    <OutputType>exe</OutputType>
    <LangVersion>9.0</LangVersion>
    <Nullable>enable</Nullable>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
  </PropertyGroup>

From command line, I ran:

c:\git\FlatSharp\samples> dotnet publish -f net5.0 -c Release

Then ran:

C:\git\FlatSharp\samples\bin\Release\net5.0\win7-x64> Samples.exe

And the program succeeded. Are these different than the steps you are following? My precise configuration is:

c:\git\FlatSharp\samples>dotnet --list-sdks --list-runtimes
3.1.410 [C:\Program Files\dotnet\sdk]
5.0.201 [C:\Program Files\dotnet\sdk]
5.0.204 [C:\Program Files\dotnet\sdk]

c:\git\FlatSharp\samples>dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Are you referring to ReadyToRun?

    <PublishReadyToRun>true</PublishReadyToRun>

<PublishSingleFile>true</PublishSingleFile> <SelfContained>true</SelfContained>

Okay -- this finally reproduces on my machine. Thanks!

FlatSharp supports two modes of operation:

  • Runtime compilation where you decorate your classes with attributes like [FlatBufferTable], and the serializer is generated and built at runtime for you.
  • Ahead of time compilation where you specify an FBS file and all of your serializers are generated at build time.

Generally, the FBS file approach gives you more flexibility and interoperability. You can find more information in the wiki, which I just updated to reflect this issue.

The FlatSharp samples use both of these approaches:

The short version of why this is failing for you is that:

  • Single file packaging breaks certain APIs. Notably, Assembly.Location does not perform as expected in this mode.
  • FlatSharp's runtime codegen depends on Assembly.Location when invoking Roslyn (C# compiler).
  • Which leads to the error you see above, since it tries to reference an empty string.

FlatSharp's runtime compilation is therefore incompatible with single file packaging, though the FBS file approach does work fine with single file packaging. Commenting out the samples that use runtime compilation allows the program to work on my machine:

            //MonsterAttributeExample.MonsterAttributeExample.Run();
            //SerializerOptions.SerializerOptionsExample.Run();
            //SchemaFilesExample.SchemaFilesExample.Run();
            SchemaFilesExample2.SchemaFilesExample2.Run();
            GrpcExample.GrpcExample.Run();
            //CopyConstructorsExample.CopyConstructorsExample.Run();
            IncludesExample.IncludesExample.Run();
            //SortedVectors.SortedVectorsExample.Run();
            Unions.UnionsExample.Run();
            SharedStrings.SharedStringsExample.Run();
            //IndexedVectors.IndexedVectorsExample.Run();
            //TypeFacades.TypeFacadesExample.Run();
            StructVectors.StructVectorsSample.Run();
            WriteThrough.WriteThroughSample.Run();

Let me know if you have more questions.