Assembly locking rules and when is shadow copy useful?
Asked Answered
W

1

6

from what I've understood so far, by reading this doc for instance : http://msdn.microsoft.com/en-us/library/ms404279.aspx, Shadow copy is a feature that allows the use of an assembly while currently loaded by an application.

From the above doc :

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

But it seems like sometimes a loaded assembly is not locked and so Shadow copy is useless.

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

using System;

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

Then I load it into an 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");
 }
}

So I expected that while the program is hanging on the ReadLine I should not be able to use the A.dll assembly, but it appears that it is not locked at all : I can even delete it !

So here are my questions :

1) Why in this sample the loaded assembly is not locked ?

2) When are assembly locked, ie when shadow copy is a useful feature ?

Thanks in advance for your help.

Wingo answered 1/9, 2010 at 22:24 Comment(2)
I tried your code and it works as expected – the assembly is locked and cannot be deleted until the test program exists.Sain
Hum, interesting, so it seems the behavior is not deterministic and can differ in different contexts. This is quite confusing.Wingo
S
4

Shadow copies are useful when app domain restarts. For eg. assume your program starts a set of plugins using its own app domains and in the background you download an updated version. If the app domain is started using shadow copy then your plugin implementation DLL can be updated and you can reload the plugin and the new version would get picked up by the appdomain restart.

Socioeconomic answered 28/4, 2011 at 5:38 Comment(1)
Thanks for this example that is a perfect illustration of the usefulness of shadow copying. But the reason why the sample worked the way it did remains a mystery.Wingo

© 2022 - 2024 — McMap. All rights reserved.