ekonbenefits / impromptu-interface

Static interface to dynamic implementation (duck casting). Uses the DLR combined with Reflect.Emit.
Apache License 2.0
655 stars 67 forks source link

[macOS] The type initializer for 'Dynamitey.Dynamic' threw an exception #25

Closed sunary closed 6 years ago

sunary commented 6 years ago

Trying wrap my service using impromptu-interface version 7.0.1-beta40. This my code:

services.AddTransient<SongRepository>();
            services.AddTransient<ISongRepository>(ctx =>
            {
                var repo = ctx.GetRequiredService<SongRepository>();
                var logger = ctx.GetRequiredService<ILogger<SongRepository>>();
                var res = new Wrapper<SongRepository>(repo, logger);

                return res.ActLike<ISongRepository>();
            });

but throw exception on Mac:

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: The type initializer for 'Dynamitey.Dynamic' threw an exception.
System.TypeInitializationException: The type initializer for 'Dynamitey.Dynamic' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'System, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type, ObjectHandleOnStack keepalive)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError)
   at Dynamitey.Dynamic..cctor()
   --- End of inner exception stack trace ---
   at Dynamitey.Dynamic.GenericDelegateType(Int32 paramCount, Boolean returnVoid)
   at ImpromptuInterface.Build.BuildProxy.GenerateCallSiteFuncType(IEnumerable`1 argTypes, Type returnType, MethodInfo methodInfo, TypeBuilder builder)
   at ImpromptuInterface.Build.BuildProxy.DefineCallsiteField(TypeBuilder builder, String name, Type returnType, Type[] argTypes)
   at ImpromptuInterface.Build.BuildProxy.MakeMethod(ModuleBuilder builder, MethodInfo info, TypeBuilder typeBuilder, Type contextType, Boolean nonRecursive, Boolean defaultImp)
   at ImpromptuInterface.Build.BuildProxy.BuildTypeHelper(ModuleBuilder builder, Type contextType, Type[] interfaces)
   at ImpromptuInterface.Build.BuildProxy.BuildType(Type contextType, Type mainInterface, Type[] otherInterfaces)
   at ImpromptuInterface.Impromptu.ActLike[TInterface](Object originalDynamic, Type[] otherInterfaces)
...

Working fine on Window, didn't test on Linux.

jbtule commented 6 years ago

Hmm, there was an old version of dynamitey that had this problem, but the current version can't throw an exception in it's class constructor. https://github.com/ekonbenefits/dynamitey/blob/60ffece7d5104d7acf759062cc21883b19c1da54/Dynamitey/Dynamic.cs#L54-L66 On mac it doesn't do strong naming, is there something else pulling in a dynamitey dependency. Check the version of Dynamitey.dll

sunary commented 6 years ago

how can I update Dynamitey?

this is my package: nuget package

jbtule commented 6 years ago

2.0.6-beta111 is the latest. Doesn't crash on CI .net core Mac unit tests. If you can find out how to reproduce this it would be appreciated. All tests pass: https://travis-ci.org/ekonbenefits/impromptu-interface/jobs/266179915

jbtule commented 6 years ago

Nevermind, while I can't reproduce it, I found where the type initializer is probably throwing. https://github.com/ekonbenefits/dynamitey/blob/60ffece7d5104d7acf759062cc21883b19c1da54/Dynamitey/Dynamic.cs#L805

jbtule commented 6 years ago

So try to update dynamitey to 2.0.6-beta114. I think that should work with semantic versioning and .net core. But let me know if you run in to trouble. Also let me know if this solves your crash. Thanks!!

https://www.nuget.org/packages/Dynamitey/2.0.6-beta114

sunary commented 6 years ago

Its worked after upgrade to Dynamitey 2.0.6-beta114. Thanks for your support!