Thursday, October 15, 2009

Compilation error CS0433: The type exists in both

Compilation error CS0433: The type exists in both
Error Message :-
The type TypeName1 exists in both TypeName2 and TypeName3
Problem :- When we try to compile our ASP.NET site, sometimes it throws the error message something like
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\myapp\6990
f87a\730687aa\App_Web_em-vsrux.6.cs(1698): error CS0433: The type 'ASP.leftMenu'
exists in both 'c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET
Files\myapp\6990f87a\730687aa\App_Web_rr7zh4b1.dll' and 'c:\WINDOWS\Microsoft.N
ET\Framework\v2.0.50727\Temporary ASP.NET Files\myapp\6990f87a\730687aa\App_Web_16rxjped.dll'

I searched it for solutions but most of the site reefers it as a known bug no apparent solution.
  • Microsoft solution :-Two different assemblies referenced in your application contain the same namespace and type, which produces ambiguity.To resolve this error, use the alias feature of the /reference (Import Metadata) (C# Compiler Options) compiler option or do not reference one of your assemblies.This is mostly likely due to a known bug in the ASP.NET compiler involving directory level circular references
  • Another solution is some sites adding batch="false" and debug="false" attributes to the element in web.config.But there are warnings it may create the following problems
  1. 1) The compilation of ASP.NET pages takes longer (since some batch optimizations are disabled)
  2. 2) Code can execute slower (since some additional debug paths are enabled)
  3. 3) Much more memory is used within the application at runtime
  4. 4) Scripts and images downloaded from the WebResources.axd handler are not cached
  • III. Another solution I found is changing something in the control code which referred in the error message, save and compile again. It solved the problem at times.
  • IV. One solution is there goto "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files" and remove the folder named your project name(but you have to stop aspnet_wp process before deleting everything in the folder. It worked fine occasionally

Permanent solution to Compiler Error CS0433

I verified the error message. The type 'ASP.leftMenu' is a User Control(.ascx) used for menu creation for every page. In our site the same menu control created in different directories for different purposes.

On top of any menu control there is one line
"<%@ Control Language="C#" ClassName="leftMenu" %>
The class name "leftMenu" is repeated in every control files. I replaced class name with unique names. It solved the problem forever.

Normally this problem occurs when more than one dll contains the same exact class reference. I can explain some condition. I have one class file in my project and later I want to modify the same file. In normal practice I rename the file to something name_old.cs and modify the orginal name.cs. But the two files will have the same exact namespace as well as class definition. We may not notice the problem. But when we try to compile or run the project, it will display error all the places where we referred the class.

You can eliminate this error by checking the same reference in more than one file.