无法使用 Activator.CreateInstance 创建 COM class 的实例

Unable to Create the instance of COM class using Activator.CreateInstance

我正在尝试使用以下代码创建 com 对象:

Type testlib = Type.GetTypeFromProgID("TestComApp.TestApp.TestClass");
dynamic comObj = Activator.CreateInstance(testlib);

但是出现错误:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Retrieving the COM class factory for component with CLSID {FD909333-3CD0-477F-8A7E-B8045B0B84EC} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154REGDB_E_CLASSNOTREG)).

我已经尝试使用 AnyPlatform、X86、x64 位平台和用于 32 位和 64 位的 RegAsm 构建项目。

但仍然无法正常工作。我已在注册表中验证 class 和带有 GUID 的 CLSID 存在

它开始工作,一旦我使用 codebase 和 Regasm

cd "C:\Windows\Microsoft.NET\Framework\v4.0.30319"

regasm /codebase C:\DATA\TestComApp\TestComApp\bin\Debug\TestComApp.DLL

早些时候我在做:

cd "C:\Windows\Microsoft.NET\Framework\v4.0.30319"

regasm C:\DATA\TestComApp\TestComApp\bin\Debug\TestComApp.DLL

我在生产模式(在 IIS 中)的 WCF 项目中遇到了同样的问题。 在开发模式下 (Visual Studio) 不会发生错误。

对我来说,解决方案是在 IIS -> 应用程序池 -> 高级设置 -> 启用 32 位应用程序中: