You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LogicalCallContext导致IsAvailable调用触发ModuleLoadException

ASP.NET MVC 应用启动时 Azure ServiceRuntime 初始化抛出类型解析异常

问题背景

  • 现有一个ASP.NET MVC Web应用,启动阶段Global.asax.cs会调用ApplicationSettings.cs中的配置加载逻辑:通过判断ServiceRuntime.RoleEnvironment.IsAvailable的取值,选择从Azure云配置或本地应用配置加载设置,该段业务逻辑本身不是问题核心。
  • 新增链路日志追踪代码后,应用启动直接抛出异常。新增代码位于Test.Common.Logging.Tracing.TracingContext.cs,实现逻辑为调用CallContext.LogicalSetData存储线程上当前请求的链路属性,用于全链路日志追踪;只要移除CallContext.LogicalSetData相关代码行,应用即可正常启动,无异常抛出。
  • 初步推测问题与AppDomain初始化机制相关:CallContext.LogicalSetData会将数据写入LogicalCallContext,而LogicalCallContext本身支持跨AppDomain传递数据,但暂无法定位异常产生的具体根因,需要对应的排查思路和解决方案。

异常详情

启动阶段抛出的完整异常信息如下:

Microsoft.WindowsAzure.ServiceRuntime Critical: 102 : Unexpcted Exception During Runtime Startup:
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
 ---> System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Test.Common.Logging.Tracing.TracingContext,Test.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.AppDomain.get_Id()
   at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
   at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   --- End of inner exception stack trace ---
   at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   at .cctor()
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeEnvironment()
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor()
[2022-06-25 22:29:43] DiagnosticLogs: 
     Message [The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception.]
     LogSeverityLevel [Error]
     InnerException [System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
 ---> System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Test.Common.Logging.Tracing.TracingContext,Test.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.AppDomain.get_Id()
   at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
   at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   --- End of inner exception stack trace ---
   at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
   at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
   at .cctor()
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeEnvironment()
   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor()]
     StackTrace [   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_IsAvailable()
   at Test.Business.Utility.ApplicationSettings.GetCloudOrAppSetting(String propertyName, String defaultPropertyValue, Boolean useCache) in C:\test\Applications\Test\Test.Business\Utility\ApplicationSettings.cs:line 58
   at Test.Business.Utility.ApplicationSettings.LoadDySettings() in C:\test\Applications\Test\Test.Business\Utility\ApplicationSettings.cs:line 109
   at TestUserProfile.Global.Application_Start(Object sender, EventArgs e) in C:\test\Applications\Test\TestUserProfile\Global.asax.cs:line 70]
     TraceActivityId [bbd73ca7-f3dd-4eef-90bc-006732c617c6]
     CorrelationId [051f3a29-62f4-4cc9-9854-8bd6fc02c08d]
     BuildNumber [1.0.0.1]
     SourceFilePath [C:\test\Applications\Test\TestUserProfile\Global.asax.cs]
     SourceMethodName [Application_Start]
     MachineName [SAURAV-SURFACEB]
[2022-06-25 22:29:43] Metric: Total Exception - 1, Dimensions: 
     Method Name [Application_Start]

核心异常特征:序列化阶段无法解析成员Test.Common.Logging.Tracing.TracingContext,Test.Common,异常触发点为Azure运行时C++模块初始化默认AppDomain的流程中。

内容的提问来源于stack exchange,提问作者SauravBhattacharya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 07:06:07