Multiple Assembly Versions in a single Web Site - How to deal with a CS0433 compiler error
Web development is all about breaking up your application into pieces. Multiple pages make up a site. UserControls are used to break up pages into smaller pieces, and custom libraries are often used for either framework value, or as GUI libraries. Any time you start working with these scenarios, assemblies and references are going to start to enter the equation.
In a perfect world, assembly references would be easy, and in many cases they are. But what happens when you need to support more complex scenarios? I happen to live and breathe components, working for a UI tools company, so I run into these questions every day.
Yesterday, I had a customer who needed to get an update for a bugfix, but wanted to limit the exposure (and regression testing) by applying the fix to a single page (or component) rather than the entire site. I happen to think this is a great idea and can dramatically reduce 'risk' when updating software.
Solution 1: Binding Redirects
Applying an update for a single assembly is easy to do, except when you have to start worrying about shared references. For instance, the Infragistics tools have a common "Shared" assembly that acts as the framework library for all other assemblies. When you want to update a single assembly (Infragistics2.WebUI.UltraWebGrid.v8.2 for example) you need to also update that assemblies referenced assemblies - specifically the "shared" assembly. But what about the other assemblies? If you replace the "shared" assembly with a new version, the other assemblies will no longer load since they can't find the version they were originally compiled with. This is where binding redirects come into play.
A Binding Redirect is a way to tell the fusion assembly loader that even though an assembly with version a.b.c.d is being requested, load a.b.c.z instead. I've covered this topic before in my Surgical Hotfixes with Binding Redirects post.
Binding Redirects are great if you want to update an assembly reference for your entire site, but in many cases this is still too broad of a change. There must be a way to apply an update to a single page - right? Luckily, the answer is yes.. but there are a few requirements. First the solution.
Solution 2: Page Specific Web.config file
Web.config files have the great ability to cascade through your site, so you can put a web.config in any folder. The Web.config file is also where you specify assembly references - how convenient! The trick is to specify assembly references specific to a page by creating a web.config for your 'special' page. Which brings us to the requirements.
You must put your 'special' page in its own directory since web.config files are organized at directory levels. You also must use the GAC for your assembly references. This was where I got tripped up. I had my original assemblies in the bin and tried creating a second page with it's own web.config all set up perfectly. The visual studio compiler doesn't like this, as it will automatically load the assemblies in the bin directory and give you the CS0433 error upon compilation. Once I deleted the local bin assemblies everything was happy.
Here's an example of a subdirectory web.config file -
And here's what my website looks like