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?

Leonid Vasilev
  • 11,910
  • 4
  • 36
  • 50
  • Have you ever found a solution to this problem? Having this problem on our project with TeamCity and the NUnit testrunner. The MSTest runners all work properly. – Jan_V Jun 30 '15 at 14:30
  • I am getting the same error, however when accessing RoleEnvironment in a normal Azure app service - could not find any solution yet - this is driving me crazy... – olee Nov 17 '17 at 14:39

3 Answers3

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.

Steve
  • 191
  • 1
  • 5
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
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
  • MS Connect is down right now (maybe it's trying to use `RoleEnvironment` with app domains :). Otherwise, I'd submit a bug. – Edward Brey Aug 01 '15 at 21:58
  • The site is back up (super putzy though - maybe lots of people reporting bugs). [Here's the bug report](https://connect.microsoft.com/VisualStudio/feedback/details/1617665) – Edward Brey Aug 02 '15 at 00:58
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

gorillapower
  • 450
  • 6
  • 15