开发者

Assembly locking rules and when is shadow copy useful?

开发者 https://www.devze.com 2023-01-13 21:51 出处:网络
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

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 s开发者_如何学Chadow copy is a useful feature ?

Thanks in advance for your help.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消