ASP.NET MVC JsonResult on Mono and the JavaScriptSerializer

I have been doing a little work on an ASP.NET MVC solution on Visual Studio and Windows and then a little later i moved it to MonoDevelop on OS X to continue development. Opening the Visual Studio solution file in MonoDevelop just works and compiling the code has no issues, however running it I got the following exception:

Cannot cast from source type to destination type.

System.InvalidCastException: Cannot cast from source type to destination type.
  at System.Web.Script.Serialization.JavaScriptSerializer..ctor (System.Web.Script.Serialization.JavaScriptTypeResolver resolver, Boolean registerConverters) [0x0000d] in /private/tmp/monobuild/build/BUILD/mono-2.10.8/mcs/class/System.Web.Extensions/System.Web.Script.Serialization/JavaScriptSerializer.cs:66 
  at System.Web.Script.Serialization.JavaScriptSerializer..cctor () [0x00000] in <filename unknown>:0 

After a little investigation I found that it is mixed up DLL's causing this. You can see on the screenshot below two versions of System.Web.Extensions.dll is loaded both 3.5 and 4.0.

System.Web.Extensions loaded twice in two different versions

The reason for this seems to be that the System.Web.Mvc.dll is compiled referencing the 3.5 version and not the 4.0 version so we can easily fix this with a redirect using assemblyBinding element in our web.config:

<runtime>
	<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
		<dependentAssembly>
			<assemblyIdentity name="System.Web.Extensions" culture="neutral" publicKeyToken="31bf3856ad364e35" />
			<bindingRedirect oldVersion="3.5.0.0" newVersion="4.0.0.0"/>
		</dependentAssembly>
	</assemblyBinding>
</runtime>

And that should make you able to proceed with your ASP.NET MVC development with Mono

UPDATE: This is already reported as a bug describing the workaround aswell: https://bugzilla.novell.com/show_bug.cgi?id=664813

Posted on 18 Jan 2012 by Jakob T. Andersen
blog comments powered by Disqus