ModuleLoadException on ServiceRuntime.RoleEnvironment.IsAvailable due to LogicalCallContext

80 Views Asked by At

I have an ASP.NET MVC web application in which I am getting an error on startup. The global asax.cs calls into ApplicationSettings.cs which has a check to ServiceRuntime.RoleEnvironment.IsAvailable to load settings from one of cloud settings or app settings (but that is not important). This is the exception thrown:

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]

This error has been happening since I added some special log tracing code which does: CallContext.LogicalSetData in Test.Common.Logging.Tracing.TracingContext.cs to track some properties of a current request on the thread. But if I remove the CallContext.LogicalSetData line, the error goes away, and the app starts up fine.

I think this has something to do with AppDomain and its initialization and the fact that CallContext.LogicalSetData sends information to a LogicalCallContext which can be in a different AppDomain. But I am not able to make sense out of it. Any help please?

1

There are 1 best solutions below

0
SauravBhattacharya On BEST ANSWER

Turns out that the class being serialized needed to extend MarshalByRefObject.

"MarshalByRefObject enables access to objects across application domain boundaries in applications that support remoting".

Since RoleEnvironment initializes a new AppDomain, any objects from LogicalContext need to be MarshalByRefObject types.

Thanks to this link where I got the hint: https://www.codeproject.com/Questions/696736/XML-Serialization-issue-when-Logical-Call-Context