jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers
Apache License 2.0
497 stars 50 forks source link

Not work after publish as single file #176

Closed gemoglobin closed 3 years ago

gemoglobin commented 3 years ago

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.GetOrCreateTypedSerializer[TRoot]() --- 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)

jamescourtney commented 3 years ago

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

Thanks!

gemoglobin commented 3 years ago

1.MonsterAttributeExample

  1. Win10 .NET 5
jamescourtney commented 3 years ago

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.

gemoglobin commented 3 years ago

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>

jamescourtney commented 3 years ago

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

jamescourtney commented 3 years ago

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]
jamescourtney commented 3 years ago

Are you referring to ReadyToRun?

    <PublishReadyToRun>true</PublishReadyToRun>
gemoglobin commented 3 years ago

`true

true`
jamescourtney commented 3 years ago

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

FlatSharp supports two modes of operation:

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:

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.