无法在新的应用程序域中加载 C 模块
我有一些 .NET 代码需要在单独的 AppDomain 中运行,并且一切顺利,除非调用 COM 组件.
为了说清楚,我写了一个非常简单的repro如下:
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();
});
例外:
System.TypeInitializationException:类型初始值设定项''抛出异常.--->.ModuleLoadException: C++ 模块失败在尝试初始化默认应用程序域时加载.--->System.Runtime.InteropServices.COMException: 操作无效.(来自 HRESULT 的异常:0x80131022)在System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32)errorCode, IntPtr errorInfo) 在.GetDefaultDomain() 在.DoCallBackInDefaultDomain(IntPtr 函数,无效* cookie)在.LanguageSupport._Initialize(LanguageSupport*) 在.LanguageSupport.Initialize(LanguageSupport*) --- 内部异常堆栈跟踪结束 --- 在.ThrowModuleLoadException(String错误消息,异常内部异常)在.LanguageSupport.Initialize(LanguageSupport*) 在 .cctor()
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()
代码可以传入默认域,应该不是.NET框架版本的问题.
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?
最后我通过添加修复它
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
在配置文件中.
链接非常有用.