Assembly blocking rules and when is a shadow copy used?

from what I understood so far by reading this document, for example: http://msdn.microsoft.com/en-us/library/ms404279.aspx , Shadow copy is a function that allows you to use the assembly, while how it is loaded by the application.

From the above document:

The total runtime of the language locks the assembly file when the assembly is loaded, so the file cannot be updated until the assembly is unloaded. The only way to unload the assembly from the application domain is to unload the application domain, so under normal circumstances the assembly cannot be updated on disk until all the application domains that use it have been unloaded. When the application domain is configured to shadow files, assemblies from the application path are copied to another location and downloaded from this location. The copy is locked, but the source assembly file is unlocked and can be updated.

But sometimes it seems that the downloaded assembly is not locked and therefore a shadow copy is useless.

To illustrate this point, I created a simple A.dll library with this code:

using System; public class A { public A() { Console.WriteLine("A"); } } 

Then I load it into AppDomain with code like the following:

 using System; using System.Reflection; class Test { static void Main() { AppDomainSetup configuration = new AppDomainSetup { ShadowCopyFiles = "false" }; AppDomain appDomain = AppDomain.CreateDomain("", null, configuration); Console.WriteLine(appDomain.ShadowCopyFiles); Assembly assembly = appDomain.Load("A"); assembly.CreateInstance("A"); Console.ReadLine(); assembly.CreateInstance("A"); } } 

Therefore, I expected that while the program is hanging on ReadLine , I will not be able to use the A.dll assembly, but it seems that it is not blocked at all: I can even delete it!

So here are my questions:

1) Why is the loaded assembly not blocked in this example?

2) When the assembly is locked, i.e. when is shadow copy a useful feature?

Thanks in advance for your help.

+6
clr assemblies appdomain shadow-copy
source share
1 answer

Shadow copies are useful when the application domain is restarted. E.g. Suppose your program launches a set of plugins using its own application domains, and in the background you download an updated version. If the application domain is started using a shadow copy, then your DLL version of the plugin implementation can be updated, and you can reload the plugin, and the new version will be restored by restarting the application.

+4
source share

All Articles