Custom identities (classes that implement System.Security.Principal.IIdentity) SerializationException in Cassini

A few days ago, I spend almost a day figuring out what went wrong with an application I was writing. Apparently a SerializationExeption can occur when executing ASP.NET code in the ASP.NET Development Server (Cassini).

Exception:
Type is not resolved for member ‘Classname,Assembly, Version=1.1.0.31, Culture=neutral, PublicKeyToken=null’.

Exception details:
System.Runtime.Serialization.SerializationException: Type is not resolved for member …

When executing the same website in IIS, everything runs fine.

For some strange reason the ASP.NET Development server loads the different assemblies in different ApplicationDomains, resulting in members that cannot be found.

There are different solutions to be found on the web, like inheriting your objects from MarshalByRefObject, but this can have a performance drop.

There is a simpler way in my opinion:
First of all specify the directory containing the assembly in the DEVPATH environment variable.
(Do this by opening the System Properties, properties of My Computer. At the bottom of the Advances tab page, you have a Environment Variables button. Just add the variable to your user only.)
SystemProperties EnvVars

Secondly, open the machine.config Normally you’d expect that web.config would do too, but that isn’t the case, it has to be machine.config! (This way, there isn’t any trace of this fix in theproject itself)
Add the following code:

<configuration>
   <runtime>
       <developmentMode developerInstallation="true"/>
   </runtime>
</configuration>

Save it, restart the ASP.NET Development server(s) and you’re good to go.

Report of the issue.

Note: there is 1 downside, sometimes, the .dll files that Visual Studio generates can get locked by Cassini. Restarting it helps.