Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3 runtime error when trying to load a 64-bit C++/CLI assembly

Update: Question updated after figuring out what was causing the ASP.NET/Razor errors.

I have an ASP.NET MVC3 project that references a C++/CLI project which is configured to compile as x64 because it has to run in Windows Azure (No AnyCPU setting for C++/CLI).

The problem is that as soon as I configure the platform settings of the C++/CLI project to x64 (In Build->Configuration Manager), build the solution, then open a random view I get a warning about ASP.NET trying to load an incorrect format assembly followed by a bunch of errors caused by Razor syntax errors.

Screenshot of some random View:
enter image description here

Screenshot of the log: VS2010 Error List

The main error is:

Warning: ASP.NET runtime error: Could not load file or assembly 'LZGEncoder' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Even though this happens, I can still compile and run the solution, and use the referenced C++/CLI assembly just fine, so this isn't blocking development but it's a real nuisance (especially when it blocks IntelliSense and I can't see if I have any real errors in my Razor code).

If I reset the platform of my C++/CLI to Win32 then all the errors disappear, but ofcourse when running the solution I get the same incorrect format error but in my MVC3 web-page.

Is this a bug in ASP.NET or have I forgotten to configure something? I was able to reproduce the errors at work and at home (with and without Azure), so I don't think it's my VS2010 installation. Thanks.

Update:
For now I created a C# wrapper directly calling the C library using P/Invoke, and it works great, so I don't even need the C++/CLI wrapper anymore, but that doesn't answer this question so I'll keep it open.

I'm suspecting it's a bug in MVC3/Razor or VS2010 though, both which have new versions coming out soon.

like image 634
Shahin Dohan Avatar asked Nov 14 '22 03:11

Shahin Dohan


1 Answers

I figured out what was causing the problem, it was the fact that the C++/CLI dll was being compiled as x64. The ASP.NET runtime doesn't seem to like that, and only plays nice with Win32 or AnyCPU platforms, even though the code does compile and run.

The only 2 platform options for a C++/CLI project are Win32 and x64, and I needed it to be x64 since it was going to be published to the Azure cloud, but that would cause these runtime errors, and on the other hand Win32 would not run on Azure at all.

What I ended up doing was creating a C# wrapper that directly calls my C library using P/Invoke, since C# project platform setting can be set to "AnyCPU", so now the errors are gone.

So in summary, if you're referencing any type of project/dll in a solution that contains Razor code, make sure it's compiled as AnyCPU or Win32, not x64.

Thanks everyone! I guess it is a bug in ASP.NET MVC3 afterall.

like image 198
Shahin Dohan Avatar answered Dec 22 '22 09:12

Shahin Dohan