Failed to load C module in new appdomain

2019-07-27 11:48发布

问题:

I have some .NET code that need to run in a separate AppDomain, and everything goes well except when there is call of COM component.
To make it clear, I wrote a very simple repro as below:

AppDomain ap = AppDomain.CreateDomain("newap");
ap.DoCallBack(
    () =>
    {
        //GroupPolicyObject is from assembly microsoft.grouppolicy.management.interop.dll
        GroupPolicyObject newGPO = new GroupPolicyObject();
    });

The exception:

System.TypeInitializationException: The type initializer for '' threw an exception. ---> .ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain. ---> System.Runtime.InteropServices.COMException: Invalid operation. (Exception from HRESULT: 0x80131022) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at .GetDefaultDomain() at .DoCallBackInDefaultDomain(IntPtr function, Void* cookie) at .LanguageSupport._Initialize(LanguageSupport* ) at .LanguageSupport.Initialize(LanguageSupport* ) --- End of inner exception stack trace --- at .ThrowModuleLoadException(String errorMessage, Exception innerException) at .LanguageSupport.Initialize(LanguageSupport* ) at .cctor()

The code can pass in default domain, and should not be the issue related with the version of .NET framework.

I did a lot of searching in the internet, but there’s no useful solution. Does anyone have ideas what’s the issue and how to achieve this?

回答1:

At last I fixed it by adding

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

in the cofig file.

The link is quite useful.



标签: c# com appdomain