What happens when I overwrite a DLL (asp.net)?

I post on Stack Overflow a lot. Sometimes, there are really interesting questions like one I answered last year. I had forgotten about it until this week when the answer was accepted. I thought I’d share it on my blog.

The original post is here. Read on for redundancy.

ASP.NET performs a thing called shadow copying on various resources including DLLs and ResX files. When a file is accessed by the framework, it is locked preventing direct access. To prevent locking files within the root/bin or root/App*_Resources (for example), it copies these resources to a predetermined directory which looks something like:

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\demo1\9b9144a7\8665ac07

Where demo1 is the name of your application and nested directories which are hashed (I assume) against the friendly name of the AppDomain for the contained resources.

For instance, I have a directory called dbresourceproviderweb from a resource provider example on MSDN written by Michelle Bustamante. Inside that directory, there are two folders: c8b872e2 and 7fc33f08. To go further, compiled resources for Ecuadorian Spanish are under …\dbresourceproviderweb\c8b872e2\97074f76\es-EC and …\dbresourceproviderweb\7fc33f08\ac65ebd3\es-EC

You can change this directory in Application_Start as explained here: AssemblyResolve event is not firing during compilation of a dynamic assembly for an aspx page.

You can turn off shadow copying in the web.config (or in machine.config):

<hostingEnvironment shadowCopyBinAssemblies="false" />

When one of these shadow copied files is updated within your application, a new AppDomain is spawned and requests in the current AppDomain are allowed to finish while all new requests are directed at the new AppDomain.

For more information on Shadow Copying and AppDomains, check out MSDN’s article on shadow copying.

Further Research…

You can modify the required length of time between file copy operations to spawn an AppDomain.

In the system.web/httpRuntime element, you can specify waitChangeNotification and maxWaitChangeNotification so that a new AppDomain isn’t spawned for every file copied. See MSDN.

Although there aren’t really examples for this behavior on MSDN, it’s good to keep as a reference for the configurability of the HttpRuntime.