System.AccessViolationException exception while accessing local resources in asp.net mvc

Today I have met a very strange behaviour of asp.net mvc.

I migrated my old asp.net mvc3 project to mvc4 and tried to run it up, but IIS Express died with an exception.

Debugging in Visual Studio did not give any information. It threw an exception on every try to access local resources.

The exception text is: An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.

And the method which caused the exception is:

public static string LocalResource(this WebViewPage page, string key)
{
    return page.ViewContext.HttpContext.GetLocalResourceObject(page.VirtualPath, key) as string;
}

I have spent a couple of hours trying to understand what may cause such a strange exception. In the end I figured out that the LocalResource method is defined in Helpers project, which is still using mvc3.

Conclusion: If you try to access local resources from a dll which references mvc3 from a mvc4 project it will end up with System.AccessViolationException exception.

Views: 1639 Comments: 0