ASP.NET MVC 3.0 Build Views as 64 bit

2019-06-21 18:44发布

问题:

I need to build an MVC 3.0 site and target x64 specifically. I'm having an issue trying to build my MVC 3.0 site with the Platform Target set to x64 and MvcBuildViews set to True. Everything builds fine until it tries to compile the views. If I set the Platform Target to AnyCPU everything will compile, but when set to x64 I get this error:

Could not load file or assembly 'Mvc64Bit' or one of its dependencies. An attempt was made to load a program with an incorrect format.

This can easily be recreated by creating a blank MVC 3.0 project, unload the project, edit the project file to set the MvcBuildViews item to "true", reload the project, change the Platform Target in the Project's Build Properties to x64, and then build.

I haven't been able to find anything about the above error online, just that it deals with mismatched DLLs (one x32, one x64) but this doesn't make sense unless the view build engine is 32 bit or something.

Any hints to point me in the right direction will be GREATLY appreciated. Thanks for reading!!

回答1:

I got a response from Microsoft on this issue. I guess what is happening is that Visual Studio calls a 32-bit compiler that compiles the website into a 64 bit DLL. After that, it calls the 32-bit compiler again for the views. The view compilation needs to load the 64 bit Web project DLLs to get information from the defined models. This is where the "Incorrect format" comes in. The 32 bit compiler tries to load the 64 bit Web project DLLs.

Now, calling the 64 bit aspnet_compiler.exe from the Visual Studio Command Prompt works perfectly. But, I guess, since Visual Studio is a 32 bit application, it can't load the 64 bit compiler. I'm not sure of any way to call the 64 bit, and even if there was a way, Visual studio probably couldn't give the nice list of errors that typically does (just an assumption there as I don't know how Visual Studio calls the compiler...a simple command line execution works, but maybe it actually loads the DLL and calls from inside the VS code)

So, my work around was to put the MVCBuildViews=true declaration inside the property. I then put MVCBuildViews=false in the 'Release|AnyCPU' propertyGroup and I just let IIS compile the views when the site first loads. It's not precompiling completely, but it will work.