dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.15k stars 4.71k forks source link

TypeInitializationException from BinaryCompatibility #8297

Closed jeremydmiller closed 4 years ago

jeremydmiller commented 7 years ago

As we've tried to upgrade the Storyteller project to netcoreapp1.1, we've hit this exception below that I assume is from mismatched assembly versions.

From the command line, this command causes the exception:

dotnet run --project src/StorytellerRunner/StorytellerRunner.csproj --configuration Release --framework netcoreapp1.1 -- run src/Specifications --validate

I'm not clear on what steps I should take to even determine how the wrong dependencies are getting into the project. Any guidance would be appreciated.

Unhandled Exception: System.TypeInitializationException: The type initializer for 'System.Runtime.Versioning.BinaryCompatibility' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.InteropServices, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)
   at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)
   at System.AppDomain.GetTargetFrameworkName()
   at System.Runtime.Versioning.BinaryCompatibility.ReadTargetFrameworkId()
   at System.Runtime.Versioning.BinaryCompatibility.get_AppWasBuiltForFramework()
   at System.Runtime.Versioning.BinaryCompatibility.BinaryCompatibilityMap..ctor()
   at System.Runtime.Versioning.BinaryCompatibility..cctor()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1](Func`4 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, Object state, TaskCreationOptions creationOptions)
   at System.Net.Dns.GetHostAddressesAsync(String hostNameOrAddress)
   at StoryTeller.Remotes.SocketConnection..ctor(Int32 port, Boolean owner, Action`2 onReceived)
   at StoryTeller.Engine.EngineAgent..ctor(Int32 port)
   at StoryTeller.Engine.EngineAgent..ctor(Int32 port, ISystem system)
   at StoryTeller.StorytellerAgent..ctor(Int32 port, ISystem system)
   at StoryTeller.StorytellerAgent.Run(String[] args, ISystem system)
   at Specifications.Program.Main(String[] args)
jkotas commented 7 years ago

Looks like duplicate of https://github.com/dotnet/coreclr/issues/7154.

jeremydmiller commented 7 years ago

@jkotas I don't think that's the case. It's a different exception, and the "fix" in that issue was to upgrade to a later preview version of the sdk, whereas I'm running on 1.0.3. I still think there's an incompatibility issue between dependencies, but I can't even determine the assembly/nuget where the BinaryCompatibility class lives to help track down the version mismatches. I can definitely see that System.Runtime.InteropServices v4.3.0 is a dependency of the app.

jkotas commented 7 years ago

The BinaryCompatibility class was internal class in System.Private.CoreLib: https://github.com/dotnet/coreclr/blob/release/1.0.0/src/mscorlib/src/System/Runtime/Versioning/BinaryCompatibility.cs. All it did was to call GetCustomAttributes() on your app .exe. You should be able to see the same problem if you add typeof(Program).GetTypeInfo().GetCustomAttributes() to your Main method.

I see that you are manually referencing the packages: https://github.com/storyteller/Storyteller/blob/netcore1.1/src/StoryTeller/StoryTeller.csproj#L65. Is there a good reason why you are doing this instead of just referencing the Microsoft.NETCore.App or NETStandard.Library metapackages? Have you tried adding explicit reference to System.Runtime.InteropServices to this list?

jeremydmiller commented 7 years ago

@jkotas Thanks for you help. I was never able to get past the phantom BinaryCompatibility problem, but switching to using Kestrel instead of raw sockets for inter-process communication got me working again. I'm closing this one.

jeremydmiller commented 7 years ago

@jkotas I still can't get past this. Now I've just got it happening deeper in the app.

This code brings out the problem by itself:

            DateTime time = DateTime.MinValue;
            DateTime.TryParse("anything", out time);
            Console.WriteLine(time);

Here's the full stack trace:

System.TypeInitializationException: The type initializer for 'System.Runtime.Versioning.BinaryCompatibility' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.InteropServices, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType)
   at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit)
   at System.AppDomain.GetTargetFrameworkName()
   at System.Runtime.Versioning.BinaryCompatibility.ReadTargetFrameworkId()
   at System.Runtime.Versioning.BinaryCompatibili
ty.get_AppWasBuiltForFramework()
   at System.Runtime.Versioning.BinaryCompatibility.BinaryCompatibilityMap..ctor()
   at System.Runtime.Versioning.BinaryCompatibility..cctor()
   --- End of inner exception stack trace ---
   at System.Globalization.DateTimeFormatInfo.InsertHash(TokenHashValue[] hashTable, String str, TokenType tokenType, Int32 tokenValue)
   at System.Globalization.DateTimeFormatInfo.AddMonthNames(TokenHashValue[] temp, String monthPostfix)
   at System.Globalization.DateTimeFormatInfo.CreateTokenHashTable()
   at System.Globalization.DateTimeFormatInfo.Tokenize(TokenType TokenMask, TokenType& tokenType, Int32& tokenValue, __DTString& str)
   at System.__DTString.GetRegularToken(TokenType& tokenType, Int32& tokenValue, DateTimeFormatInfo dtfi)
   at System.DateTimeParse.Lex(DS dps, __DTString& str, DateTimeToken& dtok, DateTimeRawInfo& raw, DateTimeResult& result, DateTimeFormatInfo& dtfi, DateTimeStyles styles)
   at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles, DateTimeResult& result)
   at System.DateTimeParse.TryParse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles, DateTime& result)
   at StoryTeller.Model.LastUpdatedConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject
(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at StoryTeller.Remotes.Messaging.JsonSerialization.DeserializeMessage(String json)
   at StoryTeller.Remotes.Messaging.MessagingHub.SendJson(String json)

And by iterating over the DependencyContext, here's what's loaded. It's pulling in a bunch of 4.0.* nugets somehow that I'm guessing is the problem, but I can't see anything that would be pulling these in.

storyteller.samples: 1.0.0
baseline: 1.4.0
microsoft.aspnetcore.hosting: 1.1.2
microsoft.aspnetcore.hosting.abstractions: 1.1.2
microsoft.aspnetcore.hosting.server.abstractions: 1.1.2
microsoft.aspnetcore.http: 1.1.2
microsoft.aspnetcore.http.abstractions: 1.1.2
microsoft.aspnetcore.http.extensions: 1.1.2
microsoft.aspnetcore.http.features: 1.1.2
microsoft.aspnetcore.server.kestrel: 1.1.2
microsoft.aspnetcore.webutilities: 1.1.2
microsoft.csharp: 4.3.0
microsoft.dotnet.internalabstractions: 1.0.0
microsoft.dotnet.platformabstractions: 1.1.2
microsoft.extensions.configuration: 1.1.2
microsoft.extensions.configuration.abstractions: 1.1.2
microsoft.extensions.configuration.environmentvariables: 1.1.2
microsoft.extensions.dependencyinjection: 1.1.1
microsoft.extensions.dependencyinjection.abstractions: 1.1.1
microsoft.extensions.dependencymodel: 1.1.2
microsoft.extensions.fileproviders.abstractions: 1.1.1
microsoft.extensions.fileproviders.physical: 1.1.1
microsoft.extensions.filesystemglobbing: 1.1.1
microsoft.extensions.logging: 1.1.2
microsoft.extensions.logging.abstractions: 1.1.2
microsoft.extensions.objectpool: 1.1.1
microsoft.extensions.options: 1.1.2
microsoft.extensions.platformabstractions: 1.1.0
microsoft.extensions.primitives: 1.1.1
microsoft.net.http.headers: 1.1.2
microsoft.netcore.platforms: 1.1.0
microsoft.netcore.targets: 1.1.0
microsoft.win32.primitives: 4.3.0
microsoft.win32.registry: 4.3.0
netstandard.library: 1.6.1
newtonsoft.json: 10.0.2
runtime.debian.8-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.fedora.23-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.fedora.24-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.native.system: 4.3.0
runtime.native.system.io.compression: 4.3.0
runtime.native.system.net.http: 4.3.0
runtime.native.system.security.cryptography.apple: 4.3.0
runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.opensuse.13.2-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.opensuse.42.1-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.osx.10.10-x64.runtime.native.system.security.cryptography.apple: 4.3.0
runtime.osx.10.10-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.rhel.7-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.ubuntu.14.04-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.ubuntu.16.04-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
runtime.ubuntu.16.10-x64.runtime.native.system.security.cryptography.openssl: 4.3.0
system.appcontext: 4.3.0
system.buffers: 4.3.0
system.collections: 4.3.0
system.collections.concurrent: 4.3.0
system.collections.immutable: 1.3.0
system.collections.nongeneric: 4.3.0
system.collections.specialized: 4.3.0
system.componentmodel: 4.3.0
system.componentmodel.primitives: 4.3.0
system.componentmodel.typeconverter: 4.3.0
system.console: 4.3.0
system.data.common: 4.3.0
system.diagnostics.contracts: 4.3.0
system.diagnostics.debug: 4.3.0
system.diagnostics.diagnosticsource: 4.3.1
system.diagnostics.process: 4.3.0
system.diagnostics.stacktrace: 4.3.0
system.diagnostics.tools: 4.3.0
system.diagnostics.tracesource: 4.3.0
system.diagnostics.tracing: 4.3.0
system.dynamic.runtime: 4.3.0
system.globalization: 4.3.0
system.globalization.calendars: 4.3.0
system.globalization.extensions: 4.3.0
system.io: 4.3.0
system.io.compression: 4.3.0
system.io.compression.zipfile: 4.3.0
system.io.filesystem: 4.3.0
system.io.filesystem.primitives: 4.3.0
system.io.filesystem.watcher: 4.3.0
system.linq: 4.3.0
system.linq.expressions: 4.3.0
system.net.http: 4.3.2
system.net.nameresolution: 4.3.0
system.net.primitives: 4.3.0
system.net.sockets: 4.3.0
system.net.websockets: 4.3.0
system.numerics.vectors: 4.3.0
system.objectmodel: 4.3.0
system.reflection: 4.3.0
system.reflection.emit: 4.3.0
system.reflection.emit.ilgeneration: 4.3.0
system.reflection.emit.lightweight: 4.3.0
system.reflection.extensions: 4.3.0
system.reflection.metadata: 1.4.1
system.reflection.primitives: 4.3.0
system.reflection.typeextensions: 4.3.0
system.resources.resourcemanager: 4.3.0
system.runtime: 4.3.0
system.runtime.compilerservices.unsafe: 4.3.0
system.runtime.extensions: 4.3.0
system.runtime.handles: 4.3.0
system.runtime.interopservices: 4.3.0
system.runtime.interopservices.runtimeinformation: 4.3.0
system.runtime.loader: 4.3.0
system.runtime.numerics: 4.3.0
system.runtime.serialization.formatters: 4.3.0
system.runtime.serialization.primitives: 4.3.0
system.security.claims: 4.3.0
system.security.cryptography.algorithms: 4.3.0
system.security.cryptography.cng: 4.3.0
system.security.cryptography.csp: 4.3.0
system.security.cryptography.encoding: 4.3.0
system.security.cryptography.openssl: 4.3.0
system.security.cryptography.primitives: 4.3.0
system.security.cryptography.x509certificates: 4.3.0
system.security.principal: 4.3.0
system.security.principal.windows: 4.3.0
system.text.encoding: 4.3.0
system.text.encoding.extensions: 4.3.0
system.text.encodings.web: 4.3.1
system.text.regularexpressions: 4.3.0
system.threading: 4.3.0
system.threading.overlapped: 4.3.0
system.threading.tasks: 4.3.0
system.threading.tasks.extensions: 4.3.0
system.threading.thread: 4.3.0
system.threading.threadpool: 4.3.0
system.threading.timer: 4.3.0
system.xml.readerwriter: 4.3.0
system.xml.xdocument: 4.3.0
system.xml.xmldocument: 4.3.0
system.xml.xmlserializer: 4.3.0
storyteller: 4.2.0
Libuv: 1.9.1
Microsoft.CodeAnalysis.Analyzers: 1.1.0
Microsoft.CodeAnalysis.Common: 1.3.0
Microsoft.CodeAnalysis.CSharp: 1.3.0
Microsoft.CodeAnalysis.VisualBasic: 1.3.0
Microsoft.NETCore.App: 1.0.5
Microsoft.NETCore.DotNetHost: 1.0.1
Microsoft.NETCore.DotNetHostPolicy: 1.0.5
Microsoft.NETCore.DotNetHostResolver: 1.0.1
Microsoft.NETCore.Jit: 1.0.7
Microsoft.NETCore.Runtime.CoreCLR: 1.0.7
Microsoft.NETCore.Windows.ApiSets: 1.0.1
Microsoft.VisualBasic: 10.0.1
runtime.any.System.Collections: 4.0.11
runtime.any.System.Diagnostics.Tools: 4.0.1
runtime.any.System.Diagnostics.Tracing: 4.1.0
runtime.any.System.Globalization: 4.0.11
runtime.any.System.Globalization.Calendars: 4.0.1
runtime.any.System.IO: 4.1.0
runtime.any.System.Reflection: 4.1.0
runtime.any.System.Reflection.Extensions: 4.0.1
runtime.any.System.Reflection.Primitives: 4.0.1
runtime.any.System.Resources.ResourceManager: 4.0.1
runtime.any.System.Runtime: 4.1.0
runtime.any.System.Runtime.Handles: 4.0.1
runtime.any.System.Runtime.InteropServices: 4.1.0
runtime.any.System.Text.Encoding: 4.0.11
runtime.any.System.Text.Encoding.Extensions: 4.0.11
runtime.any.System.Threading.Tasks: 4.0.11
runtime.any.System.Threading.Timer: 4.0.1
runtime.native.System.Net.Security: 4.0.1
runtime.native.System.Security.Cryptography: 4.0.1
runtime.win.Microsoft.Win32.Primitives: 4.0.1
runtime.win.System.Console: 4.0.0
runtime.win.System.Diagnostics.Debug: 4.0.11
runtime.win.System.IO.FileSystem: 4.0.1
runtime.win.System.Net.Primitives: 4.0.11
runtime.win.System.Net.Sockets: 4.1.0
runtime.win.System.Runtime.Extensions: 4.1.0
runtime.win7-x64.Microsoft.NETCore.DotNetHost: 1.0.1
runtime.win7-x64.Microsoft.NETCore.DotNetHostPolicy: 1.0.5
runtime.win7-x64.Microsoft.NETCore.DotNetHostResolver: 1.0.1
runtime.win7-x64.Microsoft.NETCore.Jit: 1.0.7
runtime.win7-x64.Microsoft.NETCore.Runtime.CoreCLR: 1.0.7
runtime.win7-x64.Microsoft.NETCore.Windows.ApiSets: 1.0.1
runtime.win7-x64.runtime.native.System.IO.Compression: 4.0.1
runtime.win7.System.Private.Uri: 4.0.2
System.ComponentModel.Annotations: 4.1.0
System.Diagnostics.FileVersionInfo: 4.0.0
System.IO.MemoryMappedFiles: 4.0.0
System.IO.UnmanagedMemoryStream: 4.0.1
System.Linq.Parallel: 4.0.1
System.Linq.Queryable: 4.0.1
System.Net.Requests: 4.0.11
System.Net.Security: 4.0.1
System.Net.WebHeaderCollection: 4.0.1
System.Private.Uri: 4.0.1
System.Reflection.DispatchProxy: 4.0.1
System.Resources.Reader: 4.0.0
System.Text.Encoding.CodePages: 4.0.1
System.Threading.Tasks.Dataflow: 4.6.0
System.Threading.Tasks.Parallel: 4.0.1
System.Xml.XPath: 4.0.1
System.Xml.XPath.XDocument: 4.0.1

The latest code is in master at http://github.com/storyteller/storyteller

mysticmind commented 7 years ago

If the following lines from csproj are removed, everything looks to work fine without the runtime error

<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<RuntimeFrameworkVersion Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">1.0.4</RuntimeFrameworkVersion>

I also tested the above fix on the storyteller project and confirm it to be working fine.

jkotas commented 7 years ago

That make sense. The above two lines likely introduce forced package downgrades that lead to version mismatches at runtime.