Friday, September 03, 2010

Ambiguous namespaces in while having classes App_Code directory.

If you dont need read the rest of this article and relax the rest of the day,
Solution:
Rename the App_Code folder to AppCode

Cause:
For the demonstration, I use custom type define as page base to provide common features to all pages. This base type been configured from the web.config. However this is not specific to page base type, this can happen to any type which referred at runtime from the web.config, like custom configuration classes.

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message:
The type WebNamespace.PageBase' is ambiguous: it could come from assembly 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\webnapplication\8235a0d2\f00609ba\App_Code.atbdii2e.DLL' or from assembly 'C:\Projects\WebSite\bin\WebNamespace.DLL'. Please specify the assembly explicitly in the type name.

Source Error:

Line 34:                           <error statusCode="500" redirect="error.html"/>
Line 35:                  </customErrors>
Line 36:                  <pages pageBaseType="WebNamespace.PageBase" ...>
Line 37:                           <controls>
Line 38:                                    <add tagPrefix="ajax" .../>

Why this happen?
When you have pages which does not contain specific code behind file in the solution but some scripts with run-at server in the mark-up, visual studio and asp.net create temporary DLL file in the asp.net temporary files folder (located at c:/Windows/Microsoft.NET/Framework/Asp.net-Version/Temporary Asp.net Files/ApplicationName). In such a occasion, when you add a class file to App_Code directory, which has its own name space within asp.net web application’s assembly this class get created in both temporary and fixed assembly. Then from the configuration, it any of these classes being referred you get a run-time error stating ambiguous type. This is because while asp.net creates temporary assembly for temporary classes which does not have specific file to inherit, asp.net automatically merges all the class files which are located at App_Code directory regardless of what they are, causing same class to be in two different assemblies.
Then you may wonder why I can’t specify assembly along with the type name like this.
<pages pageBaseType="WebApplication.PageBase, WebAssemblyName"...
But still no luck, .net can’t distinguish the type in conflicting assemblies.
Conclusion:
It is better to not to use App_Code folder for your custom classes which get referred at run-time.

1 comment:

David Worton said...

I have had exactly this problem and this is the first description of it I have been able to find. I will try your solution. Thank you!

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...