AxoCover icon indicating copy to clipboard operation
AxoCover copied to clipboard

The C++ module failed to load while attempting to initialize the default appdomain.

Open ansjinkim opened this issue 7 years ago • 12 comments

Hi,

I have a problem with running test with mixed assemblies native and managed.

In Test Explorer tab from Visual Studio, There is no problem.

If I try to run test in AxoCover tab from Visual Studio, test fails due to exception System.TypeInitializationException: The type initializer for '<Module>' threw an exception, with inner exception "The C++ module failed to load while attempting to initialize the default appdomain."

What is the difference when it runs from testhost.exe and from AxoCover.Host-x64.exe?

Thanks

ansjinkim avatar Apr 23 '18 09:04 ansjinkim

Could it be that you C++ code is x86? In which case you would need to change the platform in AxoCover settings.

axodox avatar Apr 23 '18 10:04 axodox

It could be also caused by the fact that the tests in AxoCover do not execute in the default appdomain.

axodox avatar Apr 23 '18 10:04 axodox

My C++/CLI dlls is targeted at x64 and platform in AxoCover's Test settings is also x64.

At first i face the problem, test was also failed in Test Explorer tab.

After searching, i found out this document.

https://developercommunity.visualstudio.com/content/problem/169359/ccli-dll-is-initialized-in-wrong-appdomain.html

So i modified the code that having static field with gcroot in a C++/CLI dlls, then test was passed.

But in AxoCover tab, after C++/CLI dll loaded, finding managed dll only in a directory that AxoCover.Host-x64.exe exists, not in my test dlls exist.

Now, i'm making a batch file that using OpenCover and ReportGenerator to confirm the code coverage and it has no problem.

Any help?

thanks in advance.

ansjinkim avatar Apr 24 '18 03:04 ansjinkim

So if understand correctly now the problem is with AxoCover not finding the DLLs if they are not in the runner's directory? That could happen if they reference other native DLLs, since the native DLL search paths are different. I could set the PATH environment var for the runner process to include the test DLL's directory, that should fix finding the native DLLs.

axodox avatar Apr 24 '18 05:04 axodox

To validate the above theory you could try to set the PATH environment variable on your machine to include the directory of your DLL, and restart Visual Studio and try if it works with AoxCover. If it does please tell me and I will make the change, if not we look for another solution.

axodox avatar Apr 24 '18 05:04 axodox

I did what you said, but nothing's changed. So it seems to be an issue not related to PATH environment var. When C++/CLI DLL loaded, maybe new AppDomain is being created for some reason. And i think this is probably a Visual Studio bug. I will try to find out more for myself.

Thank you anyway.

ansjinkim avatar Apr 24 '18 08:04 ansjinkim

Could you create a minimalistic sample project for me, which I could check to reproduce the error you encounter?

I never used AxoCover with mixed mode DLLs so far, so this is an uncharted territory for the engine.

axodox avatar Apr 24 '18 08:04 axodox

(AxoCover runs tests in separate AppDomain, if this is the issue, I could try to add the setting - this might be simple or not so simple depending on the test framework. Which test framework do you use?)

axodox avatar Apr 24 '18 08:04 axodox

This is a minimalistic sample project that i first encounter. TestDemo.zip

ansjinkim avatar Apr 25 '18 11:04 ansjinkim

Thanks, I will take a look over the weekend.

axodox avatar Apr 25 '18 12:04 axodox

I looked at your example, if I declare your variable like this: static ManagedClass^ g_managed = gcnew ManagedClass(); in your class in the header, then it works properly.

axodox avatar May 01 '18 08:05 axodox

For the tests to run in VS you need a runsettings file to be specified for running in x64 mode, I used this:

runSettings.zip

axodox avatar May 01 '18 08:05 axodox