11/04/2006

Web Application Project Issues 101: "Could Not Load Type..."

One *Extremely* common newsgroup and forum post I've seen recently revolves around "double compilation" of stuff that was left in the APP_CODE folder when a project is migrated from WebSite mode to Web Application Project. For starters, I'll quote directly from "Mr. ASP.NET" Scott Guthrie's blog tutorial:

"VERY, VERY IMPORTANT: Because ASP.NET 2.0 tries to dynamically compile any classes it finds under the /App_Code directory of an application at runtime, you explictly *DO NOT* want to store classes that you compile as part of your VS 2005 Web Application Project under an "app_code" folder. If you do this, then the class will get compiled twice -- once as part of the VS 2005 Web Application Project assembly, and then again at runtime by ASP.NET. The result will most likely be a "could not load type" runtime exception -- caused because you have duplicate type names in your application. Instead, you should store your class files in any other directory of your project other than one named "app_code". This will be handled automatically by the "Convert to Web Application" command. This command will rename the folder Old_App_Code."

This should be very clear, but apparently (as one would expect) a lot of developers don't take the time to read the tutorials that kind Mr. Guthrie has literally toiled over for your benefit! I should mention as a footnote that you need to choose the context menu item "Convert to Web Application Project" at the PROJECT level in your Solution Explorer node in order for this to occur.

There's another little "gotcha" that I bet you have run into, and there's a fix for that too, if you just take the time to "RTFM":

VS 2005 Web Application Projects don't automatically support generating a strongly-typed Profile class proxy. However, you can use this free download to automatically keep your own proxy class in sync with the profile configuration. Now, I'll be the first to admit that this little guy can be very tricky in getting "Profile.Common" to come alive, but once you understand what it is doing, you'll have taken the first step to "Conversion"!

Moral of the story: Not to sound like a broken MP3, but "Read the Manual" (in this case, the nice tutorials!). Or, phrased another way: There are two kinds of professional developers in the world: Those that take the time to read the manual, -- and those that take the time to read the manual.


There is no other way. More later.