Compilation Error The type 'ASP.global_asax' exists in both DLLs
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When developing ASP.NET applications, especially when dealing with code-behind files and dynamic compilation, you might encounter the error: "Compilation Error: The type 'ASP.global_asax' exists in both DLLs." This error is indicative of a conflict arising from multiple assemblies containing the same type definition, typically due to build and deployment inconsistencies.
Causes of the Error
- Duplicate Assemblies:
- The error commonly indicates that there are duplicate assemblies in the `bin` directory.
- This usually happens when multiple compilations generate DLLs with similar or duplicate type definitions, particularly the `global.asax` file.
- Incorrect Build Process:
- The build process might be incorrectly configured, generating unnecessary or redundant versions of the dynamic assemblies.
- Framework Errors:
- Errors in the application framework or in dependencies where multiple versions are loaded can also cause this issue.
- Deployment Artifacts:
- Stale files or incorrectly managed deployment artifacts can lead to leftover assembly files that conflict with newly deployed ones.
Technical Explanation
ASP.NET Compilation Process
When an ASP.NET web application starts, it compiles the different components of the site (like `global.asax`, code-behind files, etc.) into assemblies dynamically, which are stored in a temporary directory under ASP.NET's temporary files. Each type, like `ASP.global_asax`, is compiled into a particular assembly based on namespaces and class definitions.
DLL Conflict Details
The error suggests that two assemblies in the bin directory have conflicting definitions for the same class `ASP.global_asax`. ASP.NET attempts to load both, but the runtime fails with this error because it cannot determine which version of the class to use.
Resolution Strategies
- Cleanup Temporary ASP.NET Files:
- Navigate to the relevant temporary ASP.NET files directory and delete old files.
- Ensure your project does not reference multiple versions of the same assembly.
- Check your deployment process to avoid retaining old DLLs in the server's `bin` directory, possibly using automated deployment tools or scripts to clean up the directory before deploying.
- Ensure that your `global.asax.cs` file does not inadvertently introduce type duplications across multiple namespaces that could lead to redundant conflict.
- A clean and rebuild operation in your IDE typically addresses simple caching or temporary file issues.
- Although less likely, verify if changes in `web.config` are unintentionally causing redefinitions.

