Overcoming a classic design mode error when developing your own controls

Let's suppose that you are creating a new control. You have a separate project file and its referenced with a Web Application that intends to use your control. You have a ControlDesigner class included within that assembly.

Strange things start happening in design mode views hosting your controls.

1. Your control shows an error message indicating that it does not know about a property declared in the ASP.NET markup.

2. Your control shows a typecaset error message. It specifies two types that are identical or clearly polymorphic.

3. Container controls, like UpdatePanel, show an error message.

This problem can be found in Visual Studio 2005, 2008 and 2010 Beta 2.

How to cause it?

Compile your solution.

View a web form containing the control in design mode in Visual Studio.

Run the app and exit it.

Compile your solution again.

Now any web form in design mode should have this problem. You may need to use the View; Refresh command or restart Visual Studio.

It will stay this way until you fix the problem, described later.

What's happening?

Visual Studio appears to keep two copies of the web control assembly around. One is used for compiling. One is used by the Designer.

Initially these two assemblies are the same file. But after running it, design mode starts working with a different one from what was used to run the app.

Design mode requests your control to draw itself. The object it creates comes from one of those assemblies. It tries to use your ControlDesigner class, but it is passed to the ControlDesigner class of the other assembly. At this point, while you know the class types are identical, the CLR sees them as different. Each Type definition has an associated Assembly reference and those references differ.

Sometimes you will get a lengthy error message that identifies the full paths to each assembly, proving the point. Here is one using my ValidationSummary control in the PeterBlum.DES.dll.

[A]PeterBlum.DES.ValidationSummary cannot be cast to [B]PeterBlum.DES.ValidationSummary. Type A originates from 'PeterBlum.DES, Version=4.0.5.5001, Culture=neutral, PublicKeyToken=cb5182303c90db58' in the context 'LoadNeither' at location 'C:\Users\Peter\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies\oqmz8zmi01\peterblum.des.dll'. Type B originates from 'PeterBlum.DES, Version=4.0.5.5001, Culture=neutral, PublicKeyToken=cb5182303c90db58' in the context 'LoadNeither' at location 'C:\Users\Peter\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies\slrfc0r001\PeterBlum.DES.dll'.

Notice the two paths are the same up to the ProjectAssemblies folder. 

How do I fix this?

I have encountered this almost daily since I started using VS2005. I've learned that the ONLY reliable solution is as follows:

1. Get all open Visual Studio editor windows out of design mode. Either close them or switch to source view.

2. Use the Build; Clean Solution command. If its not present, add it to the menus using the Tools; Customize command. Unfortunately it does not appear unless there is a solution file. If there is not, use step 5.

3. Restart Visual Studio

4. Use the Build; Rebuild Solution command.

5. If step 2 did not have a Build; Clean Solution command, exit Visual Studio. Delete the ProjectAssemblies folder. Restart VS. Use Rebuild Solution.

FWIW: I'm so used to running these steps, I don't think about it anymore. But I did submit a bug report way back in VS2005 Beta 2 (ID=114968)

 

No Comments