I have a ServerCom DLL that comes from Fortran. I generate automatically using tlbimp a MyFortran.dll from the ServerCom.dll that can be referenced directly from C#.
In a C# Class Library I have referenced MyFortran.dll.
I created a console application that use the MyFortran.dll and generated the correct manifest (in order to have a free-interopt COM environment).
It works perfectly in the console application.
Now, I wrote a simple NUnit test and I got a COM Exception.
System.Runtime.InteropServices.COMException : Retrieving the COM class factory for component with CLSID {0FB0F699-4EF8-4732-B98E-C088825E3912} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
How can I solve this?
Thanks, Adrien.
Yes, this doesn't work. The registry-free COM manifest needs to be embedded in the EXE that uses the COM server. Easy enough for your console app. Not easy when you use NUnit because the EXE is now the unit test runner. You can't/shouldn't mess with it. Hard to do anyway because there are a bunch of them.
Just don't bother, this is a deployment detail that's not relevant for the testing you want to do. Just register the server with regsvr32.exe on the machine that executes the tests and be done with it.
Check out this answer:
How to do registration-free COM in a plug-in architecture
Eugene indicates that this is possible in one of two ways:
1. embed the manifest in the dll and compile with ISOLATION_AWARE_ENABLED
2. use context activation APIs
You can use Activation Context API's to achieve this. This blog post gives all the details.
Here's a summary.
Paste the following code into your project (thanks to Spike McLarty for this):
Every time you need to create a COM object (COMObject in this example), wrap the call that creates it in a lambda function, and pass that to UsingManifestDo, like this: