Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeInitializationException when calling HostBuilder.Build() #88518

Closed
ksharperd opened this issue Jul 7, 2023 · 4 comments
Closed

TypeInitializationException when calling HostBuilder.Build() #88518

ksharperd opened this issue Jul 7, 2023 · 4 comments

Comments

@ksharperd
Copy link

Description

when i try to start my application, this exception throws immediately and causes the application to crash

note: if i don't attach my application to debugger, these message will appear

Unhandled exception.
   Cannot print exception string because Exception.ToString() failed.

if i do attach to debugger, we will get these

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Microsoft.Extensions.Logging.EventSource.LoggingEventSource' threw an exception.
  Source=Microsoft.Extensions.Logging.EventSource
  StackTrace:
   at Microsoft.Extensions.Logging.EventSourceLoggerFactoryExtensions.AddEventSourceLogger(ILoggingBuilder builder)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.<>c__DisplayClass16_0.<AddDefaultServices>b__0(ILoggingBuilder logging)
   at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.AddDefaultServices(HostBuilderContext hostingContext, IServiceCollection services)
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   ...

  This exception was originally thrown at this call stack:
    System.ModuleHandle.ResolveTypeHandle(int, System.RuntimeTypeHandle[], System.RuntimeTypeHandle[])
    System.Reflection.RuntimeModule.ResolveType(int, System.Type[], System.Type[])
    System.Reflection.CustomAttribute.FilterCustomAttributeRecord(System.Reflection.MetadataToken, System.Reflection.MetadataImport, System.Reflection.RuntimeModule, System.Reflection.MetadataToken, System.RuntimeType, bool, ref System.RuntimeType.ListBuilder<object>, out System.RuntimeType, out System.IRuntimeMethodInfo, out bool)
    System.Reflection.CustomAttribute.AddCustomAttributes(ref System.RuntimeType.ListBuilder<object>, System.Reflection.RuntimeModule, int, System.RuntimeType, bool, System.RuntimeType.ListBuilder<object>)
    System.Reflection.CustomAttribute.GetCustomAttributes(System.Reflection.RuntimeModule, int, int, System.RuntimeType)
    System.Reflection.CustomAttribute.GetCustomAttributes(System.RuntimeType, System.RuntimeType, bool)
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type, bool)
    System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type, bool)
    System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(System.Reflection.MemberInfo, System.Type, System.Diagnostics.Tracing.EventManifestOptions)
    System.Diagnostics.Tracing.EventSource.GetGuid(System.Type)
    ...
    [Call Stack Truncated]

Inner Exception 1:
TypeLoadException: Could not load type 'System.Runtime.CompilerServices.NullableContextAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags)
   at System.Diagnostics.Tracing.EventSource.GetGuid(Type eventSourceType)
   at System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings, String[] traits)
   at Microsoft.Extensions.Logging.EventSource.LoggingEventSource..ctor()
   at Microsoft.Extensions.Logging.EventSource.LoggingEventSource..cctor()

Reproduction Steps

1.Download and install .Net 8 nightly build from https://github.com/dotnet/installer#table
2.Create a Console Application and write code to call Microsoft.Extensions.Hosting.HostBuilder.Build()
3.After build and run this application, this error will occur

Expected behavior

no errors occur and the application runs smoothly

Actual behavior

an exception throws immediately and causes the application to fail

Regression?

this problem did not occur in previous builds

Known Workarounds

No response

Configuration

.NET Version

Microsoft.NETCore.App

8.0.0-preview.7.23356.10

Microsoft.Extensions.Hosting

8.0.0-preview.7.23356.16

OS Infomation

Version

Windows 11 Build 10.0.25393

Arch

x64

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 7, 2023
@ghost
Copy link

ghost commented Jul 7, 2023

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

when i try to start my application, this exception throws immediately and causes the application to crash

note: if i don't attach my application to debugger, these message will appear

Unhandled exception.
   Cannot print exception string because Exception.ToString() failed.

if i do attach to debugger, we will get these

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Microsoft.Extensions.Logging.EventSource.LoggingEventSource' threw an exception.
  Source=Microsoft.Extensions.Logging.EventSource
  StackTrace:
   at Microsoft.Extensions.Logging.EventSourceLoggerFactoryExtensions.AddEventSourceLogger(ILoggingBuilder builder)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.<>c__DisplayClass16_0.<AddDefaultServices>b__0(ILoggingBuilder logging)
   at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.AddDefaultServices(HostBuilderContext hostingContext, IServiceCollection services)
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   ...

  This exception was originally thrown at this call stack:
    System.ModuleHandle.ResolveTypeHandle(int, System.RuntimeTypeHandle[], System.RuntimeTypeHandle[])
    System.Reflection.RuntimeModule.ResolveType(int, System.Type[], System.Type[])
    System.Reflection.CustomAttribute.FilterCustomAttributeRecord(System.Reflection.MetadataToken, System.Reflection.MetadataImport, System.Reflection.RuntimeModule, System.Reflection.MetadataToken, System.RuntimeType, bool, ref System.RuntimeType.ListBuilder<object>, out System.RuntimeType, out System.IRuntimeMethodInfo, out bool)
    System.Reflection.CustomAttribute.AddCustomAttributes(ref System.RuntimeType.ListBuilder<object>, System.Reflection.RuntimeModule, int, System.RuntimeType, bool, System.RuntimeType.ListBuilder<object>)
    System.Reflection.CustomAttribute.GetCustomAttributes(System.Reflection.RuntimeModule, int, int, System.RuntimeType)
    System.Reflection.CustomAttribute.GetCustomAttributes(System.RuntimeType, System.RuntimeType, bool)
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type, bool)
    System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type, bool)
    System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(System.Reflection.MemberInfo, System.Type, System.Diagnostics.Tracing.EventManifestOptions)
    System.Diagnostics.Tracing.EventSource.GetGuid(System.Type)
    ...
    [Call Stack Truncated]

Inner Exception 1:
TypeLoadException: Could not load type 'System.Runtime.CompilerServices.NullableContextAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags)
   at System.Diagnostics.Tracing.EventSource.GetGuid(Type eventSourceType)
   at System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings, String[] traits)
   at Microsoft.Extensions.Logging.EventSource.LoggingEventSource..ctor()
   at Microsoft.Extensions.Logging.EventSource.LoggingEventSource..cctor()

Reproduction Steps

1.Download and install .Net 8 nightly build from https://github.com/dotnet/installer#table
2.Create a Console Application and write code to call Microsoft.Extensions.Hosting.HostBuilder.Build()
3.After build and run this application, this error will occur

Expected behavior

no errors occur and the application runs smoothly

Actual behavior

an exception throws immediately and causes the application to fail

Regression?

this problem did not occur in previous builds

Known Workarounds

No response

Configuration

.NET Version

Microsoft.NETCore.App

8.0.0-preview.7.23356.10

Microsoft.Extensions.Hosting

8.0.0-preview.7.23356.16

OS Infomation

Version

Windows 11 Build 10.0.25393

Arch

x64

Other information

No response

Author: sakuya-lzayoi
Assignees: -
Labels:

area-System.Reflection, untriaged

Milestone: -

@ghost
Copy link

ghost commented Jul 7, 2023

Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

when i try to start my application, this exception throws immediately and causes the application to crash

note: if i don't attach my application to debugger, these message will appear

Unhandled exception.
   Cannot print exception string because Exception.ToString() failed.

if i do attach to debugger, we will get these

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Microsoft.Extensions.Logging.EventSource.LoggingEventSource' threw an exception.
  Source=Microsoft.Extensions.Logging.EventSource
  StackTrace:
   at Microsoft.Extensions.Logging.EventSourceLoggerFactoryExtensions.AddEventSourceLogger(ILoggingBuilder builder)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.<>c__DisplayClass16_0.<AddDefaultServices>b__0(ILoggingBuilder logging)
   at Microsoft.Extensions.DependencyInjection.LoggingServiceCollectionExtensions.AddLogging(IServiceCollection services, Action`1 configure)
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.AddDefaultServices(HostBuilderContext hostingContext, IServiceCollection services)
   at Microsoft.Extensions.Hosting.HostBuilder.InitializeServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   ...

  This exception was originally thrown at this call stack:
    System.ModuleHandle.ResolveTypeHandle(int, System.RuntimeTypeHandle[], System.RuntimeTypeHandle[])
    System.Reflection.RuntimeModule.ResolveType(int, System.Type[], System.Type[])
    System.Reflection.CustomAttribute.FilterCustomAttributeRecord(System.Reflection.MetadataToken, System.Reflection.MetadataImport, System.Reflection.RuntimeModule, System.Reflection.MetadataToken, System.RuntimeType, bool, ref System.RuntimeType.ListBuilder<object>, out System.RuntimeType, out System.IRuntimeMethodInfo, out bool)
    System.Reflection.CustomAttribute.AddCustomAttributes(ref System.RuntimeType.ListBuilder<object>, System.Reflection.RuntimeModule, int, System.RuntimeType, bool, System.RuntimeType.ListBuilder<object>)
    System.Reflection.CustomAttribute.GetCustomAttributes(System.Reflection.RuntimeModule, int, int, System.RuntimeType)
    System.Reflection.CustomAttribute.GetCustomAttributes(System.RuntimeType, System.RuntimeType, bool)
    System.Attribute.GetCustomAttributes(System.Reflection.MemberInfo, System.Type, bool)
    System.Attribute.GetCustomAttribute(System.Reflection.MemberInfo, System.Type, bool)
    System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(System.Reflection.MemberInfo, System.Type, System.Diagnostics.Tracing.EventManifestOptions)
    System.Diagnostics.Tracing.EventSource.GetGuid(System.Type)
    ...
    [Call Stack Truncated]

Inner Exception 1:
TypeLoadException: Could not load type 'System.Runtime.CompilerServices.NullableContextAttribute' from assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
   at System.Attribute.GetCustomAttributes(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
   at System.Diagnostics.Tracing.EventSource.GetCustomAttributeHelper(MemberInfo member, Type attributeType, EventManifestOptions flags)
   at System.Diagnostics.Tracing.EventSource.GetGuid(Type eventSourceType)
   at System.Diagnostics.Tracing.EventSource..ctor(EventSourceSettings settings, String[] traits)
   at Microsoft.Extensions.Logging.EventSource.LoggingEventSource..ctor()
   at Microsoft.Extensions.Logging.EventSource.LoggingEventSource..cctor()

Reproduction Steps

1.Download and install .Net 8 nightly build from https://github.com/dotnet/installer#table
2.Create a Console Application and write code to call Microsoft.Extensions.Hosting.HostBuilder.Build()
3.After build and run this application, this error will occur

Expected behavior

no errors occur and the application runs smoothly

Actual behavior

an exception throws immediately and causes the application to fail

Regression?

this problem did not occur in previous builds

Known Workarounds

No response

Configuration

.NET Version

Microsoft.NETCore.App

8.0.0-preview.7.23356.10

Microsoft.Extensions.Hosting

8.0.0-preview.7.23356.16

OS Infomation

Version

Windows 11 Build 10.0.25393

Arch

x64

Other information

No response

Author: sakuya-lzayoi
Assignees: -
Labels:

area-System.Reflection, area-System.Runtime.CompilerServices, untriaged

Milestone: -

@jkotas
Copy link
Member

jkotas commented Jul 7, 2023

This is related to #87857 cc @ericstj @stephentoub

This exception means that assemblies built before this change are mixed and matched with assemblies built after this change. This incoherency is "by design" in our current process for producing nightly builds of the SDK. The root cause will be addressed by https://github.com/dotnet/arcade/tree/main/Documentation/UnifiedBuild project in .NET 9.

This issue with go way on its own in several days. Please wait a week for this change to propagate to all places and then try again.

@jkotas jkotas closed this as completed Jul 7, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 7, 2023
@ericstj
Copy link
Member

ericstj commented Jul 7, 2023

Yes, this is due to a mismatch between the runtime in the SDK installer and the runtime required by the nightly built Microsoft.Extensions packages. Any change where a package depends on API we've added in the runtime will face this same problem. As @jkotas mentions you can wait for our runtime changes to flow up to the installer and install a new nightly build. Alternatively you can install the nightly runtime, which would upgrade the one installed by the SDK. Or just use a slightly older build of Microsoft.Extensions that matches the build of runtime which is in the SDK you installed.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants