Failed to load C module in new appdomain

梦想的初衷 提交于 2019-12-11 01:14:02

问题


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.



来源:https://stackoverflow.com/questions/20111881/failed-to-load-c-module-in-new-appdomain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!