What causes ModuleLoadException in RoleEnvironment initializer?

4

I'm debugging a class library with the WCF service, which uses installed Azure SDK 2.4 on Windows 8.1 using WCF Test Client and Visual Studio 2013 Ultimate debugger. The library was compiled using AnyCPU. An exception occurs when RoleEnvironment.IsAvailable is called. Here are the details:

  Message=The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment'   threw an exception.
  ...
  InnerException: System.TypeInitializationException
       Message=The type initializer for '<Module>' threw an exception.
       Source=Microsoft.WindowsAzure.ServiceRuntime
       TypeName=<Module>
       StackTrace:
            at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeEnvironment()
            at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor()
       InnerException: <CrtImplementationDetails>.ModuleLoadException
            HResult=-2146233088
            Message=The C++ module failed to load while attempting to initialize the default appdomain.
            Source=msshrtmi
            StackTrace:
                 at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
                 at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
                 at .cctor()
            InnerException: System.Runtime.InteropServices.COMException
                 HResult=-2146234334
                 Message=Invalid operation. (Exception from HRESULT: 0x80131022)
                 Source=mscorlib
                 ErrorCode=-2146234334
                 StackTrace:
                      at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
                      at <CrtImplementationDetails>.GetDefaultDomain()
                      at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
                      at <CrtImplementationDetails>.DefaultDomain.Initialize()
                      at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* )
                      at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
                      at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )

Enabling of unmanaged code debugging gave me no new information. I tried to install the last version of Azure SDK, adding useLegacyV2RuntimeActivationPolicy="true" flag to config file, but none of this helped. The same library code invoked from my test project works without exceptions. Using both x86 and x64 test settings. Debug folders seem to contain the exact same version of ServiceRuntime dll.

What caused this exception, and how I can fix it?

c#
wcf
visual-studio-2013
windows-8.1
azure-sdk-.net
asked on Stack Overflow Apr 30, 2015 by Leonid Vasilev • edited Apr 30, 2015 by Leonid Vasilev

3 Answers

5

I found a workaround to an issue I had similar to yours (identical to the MS Connect bug)

Win10, VS2015. Using Fusion Log Viewer I found my VS test runner is te.processhost.managed.exe. Following similar instructions found here, I added the following to C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\TE.processHost.managed.exe.config

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

Restarted Visual Studio and the tests started working.

answered on Stack Overflow Aug 25, 2015 by Steve
1

It looks like a new bug related to the app domains. I saw it when accessing RoleEnvironment from MSTest, which uses app domains to isolate the tests. Here's the stack trace:

msshrtmi.dll!<Module>.<CrtImplementationDetails>.GetDefaultDomain() Unknown
msshrtmi.dll!<Module>.<CrtImplementationDetails>.DoCallBackInDefaultDomain( function,  cookie)  Unknown
msshrtmi.dll!<Module>.<CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(<CrtImplementationDetails>.LanguageSupport* value)  Unknown
msshrtmi.dll!<Module>.<CrtImplementationDetails>.LanguageSupport._Initialize(<CrtImplementationDetails>.LanguageSupport* value) Unknown
msshrtmi.dll!<Module>.<CrtImplementationDetails>.LanguageSupport.Initialize(<CrtImplementationDetails>.LanguageSupport* value)  Unknown
msshrtmi.dll!<Module>.<Module>()    Unknown
[Native to Managed Transition]  
clrjit.dll!Compiler::impResolveToken(unsigned char const *,struct CORINFO_RESOLVED_TOKEN *,enum CorInfoTokenKind)   Unknown
clrjit.dll!Compiler::impImportBlockCode(struct BasicBlock *)    Unknown
clrjit.dll!Compiler::impImportBlock(struct BasicBlock *)    Unknown
clrjit.dll!Compiler::impImport(struct BasicBlock *) Unknown
clrjit.dll!Compiler::compCompile(void * *,unsigned long *,unsigned int) Unknown
clrjit.dll!Compiler::compCompileHelper(struct CORINFO_MODULE_STRUCT_ *,class ICorJitInfo *,struct CORINFO_METHOD_INFO *,void * *,unsigned long *,unsigned int,enum CorInfoInstantiationVerification)    Unknown
clrjit.dll!Compiler::compCompile(struct CORINFO_METHOD_STRUCT_ *,struct CORINFO_MODULE_STRUCT_ *,class ICorJitInfo *,struct CORINFO_METHOD_INFO *,void * *,unsigned long *,unsigned int)    Unknown
clrjit.dll!jitNativeCode(struct CORINFO_METHOD_STRUCT_ *,struct CORINFO_MODULE_STRUCT_ *,class ICorJitInfo *,struct CORINFO_METHOD_INFO *,void * *,unsigned long *,unsigned int,void *) Unknown
clrjit.dll!CILJit::compileMethod(class ICorJitInfo *,struct CORINFO_METHOD_INFO *,unsigned int,unsigned char * *,unsigned long *)   Unknown
[Managed to Native Transition]  
clrjit.dll!Compiler::impResolveToken(unsigned char const *,struct CORINFO_RESOLVED_TOKEN *,enum CorInfoTokenKind)   Unknown
clrjit.dll!Compiler::impImportBlockCode(struct BasicBlock *)    Unknown
clrjit.dll!Compiler::impImportBlock(struct BasicBlock *)    Unknown
clrjit.dll!Compiler::impImport(struct BasicBlock *) Unknown
clrjit.dll!Compiler::compCompile(void * *,unsigned long *,unsigned int) Unknown
clrjit.dll!Compiler::compCompileHelper(struct CORINFO_MODULE_STRUCT_ *,class ICorJitInfo *,struct CORINFO_METHOD_INFO *,void * *,unsigned long *,unsigned int,enum CorInfoInstantiationVerification)    Unknown
clrjit.dll!Compiler::compCompile(struct CORINFO_METHOD_STRUCT_ *,struct CORINFO_MODULE_STRUCT_ *,class ICorJitInfo *,struct CORINFO_METHOD_INFO *,void * *,unsigned long *,unsigned int)    Unknown
clrjit.dll!jitNativeCode(struct CORINFO_METHOD_STRUCT_ *,struct CORINFO_MODULE_STRUCT_ *,class ICorJitInfo *,struct CORINFO_METHOD_INFO *,void * *,unsigned long *,unsigned int,void *) Unknown
clrjit.dll!CILJit::compileMethod(class ICorJitInfo *,struct CORINFO_METHOD_INFO *,unsigned int,unsigned char * *,unsigned long *)   Unknown
Microsoft.WindowsAzure.ServiceRuntime.dll!Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.RoleEnvironment()   Unknown
answered on Stack Overflow Aug 1, 2015 by Edward Brey
1

For anyone else having this issue when trying execute a Test task in Visual Studio Team Services (VSTS), include a custom settings file in your build and set the path location on the task definition. Make and set the ForcedLegacyMode attribute = true. This solved the issue for me.

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
      <TargetFrameworkVersion>Framework45</TargetFrameworkVersion>  
  <MSTest>
     <ForcedLegacyMode>true</ForcedLegacyMode> 
  </MSTest>
</RunSettings>

For documentation on other settings and properties for Visual Studio Test files see https://msdn.microsoft.com/library/jj635153.aspx?f=255&MSPPError=-2147217396.

See the VSTS Task documentation for the Test Task here enter link description here

answered on Stack Overflow Sep 22, 2016 by gorillapower

User contributions licensed under CC BY-SA 3.0