AppDomain 应用程序域
应用程序域
一.什么是应用程序域?
二.什么时候用应用程序域?
在一个应用程序中出现的错误不会影响其他应用程序。因为类型安全的代码不会导致内存错误,所以使用应用程序域可以确保在一个域中运行的代码不会影响进程中的其他应用程序。
能够在不停止整个进程的情况下停止单个应用程序。使用应用程序域使您可以卸载在单个应用程序中运行的代码。
注意 不能卸载单个程序集或类型。只能卸载整个域。
三.应用程序域的简单实例?
SetupInfo设置程序域的信息
1 [Serializable] 2 public class SetupInfo : MarshalByRefObject 3 { 4 /// <summary>应用程序域id</summary> 5 public string Id { get; set; } 6 7 /// <summary>应用目录</summary> 8 public string BinaryDirectory { get; set; } 9 10 /// <summary>应用父目录</summary> 11 public string BaseDirectory { get; set; } 12 13 /// <summary>应用入点</summary> 14 public string EntryPoint { get; set; } 15 }
ProxyObject代理对象
1 [Serializable] 2 public class ProxyObject : MarshalByRefObject 3 { 4 public Assembly assembly; 5 6 public object instance; 7 8 public Type currentType; 9 10 public void Initialize() 11 { 12 var setupInfo = AppDomain.CurrentDomain.GetData("SETUPINFO") as SetupInfo; 13 14 string entryClass = "ZLP.AWSW.Person"; 15 string assemblyName = "ZLP.AWSW" + ".dll"; 16 string assemblyPath = Path.Combine(setupInfo.BinaryDirectory, assemblyName); 17 if (!File.Exists(assemblyPath)) 18 { 19 return; 20 } 21 assembly = Assembly.LoadFrom(assemblyPath); 22 instance = assembly.CreateInstance(entryClass); 23 currentType = instance.GetType(); 24 var methinfo = currentType.GetMethod("Execute"); 25 methinfo.Invoke(instance, null); 26 } 27 }
Proxy代理
1 [Serializable] 2 public class Proxy : MarshalByRefObject 3 { 4 public Proxy(SetupInfo setupInfo) 5 { 6 this.SetupInfo = setupInfo; 7 this.Id = setupInfo.Id; 8 } 9 10 public string Id { get; set; } 11 12 public AppDomain Domain { get; set; } 13 14 public ProxyObject ProxyObject { get; set; } 15 16 public SetupInfo SetupInfo { get; set; } 17 18 public void Start() 19 { 20 if (Domain == null) 21 { 22 Domain = CreateDomain(); 23 } 24 if (ProxyObject == null) 25 { 26 ProxyObject = CreateProxyObject(); 27 } 28 } 29 30 public AppDomain CreateDomain() 31 { 32 var setup = new AppDomainSetup(); 33 34 var cachePath = AppDomain.CurrentDomain.SetupInformation.CachePath; 35 var configurationFile = Path.Combine(SetupInfo.BinaryDirectory, "app.config"); 36 setup.ApplicationBase = SetupInfo.BaseDirectory; 37 setup.PrivateBinPath = SetupInfo.BinaryDirectory; 38 setup.ShadowCopyFiles = "true"; 39 setup.ApplicationName = SetupInfo.Id; 40 setup.ShadowCopyDirectories = string.Format("{0};{1}", SetupInfo.BaseDirectory, SetupInfo.BinaryDirectory); 41 setup.CachePath = cachePath; 42 setup.LoaderOptimization = LoaderOptimization.MultiDomainHost; 43 if (File.Exists(configurationFile)) 44 { 45 setup.ConfigurationFile = configurationFile; 46 } 47 AppDomain domain = AppDomain.CreateDomain(setup.ApplicationName, AppDomain.CurrentDomain.Evidence, setup); 48 domain.DoCallBack(new CrossAppDomainDelegate(delegate 49 { 50 LifetimeServices.LeaseTime = TimeSpan.Zero; 51 })); 52 domain.SetData("SETUPINFO", SetupInfo); 53 domain.DomainUnload += new EventHandler(DomainUnload); 54 domain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException); 55 return domain; 56 } 57 58 public ProxyObject CreateProxyObject() 59 { 60 Type type = typeof(ProxyObject); 61 Assembly currentAssembly = Assembly.GetExecutingAssembly(); 62 ProxyObject proxyObject = Domain.CreateInstanceAndUnwrap(currentAssembly.FullName, type.FullName) as ProxyObject; 63 currentAssembly = null; 64 type = null; 65 return proxyObject; 66 } 67 68 private void UnhandledException(object sender, UnhandledExceptionEventArgs e) 69 { 70 71 } 72 73 private void DomainUnload(object sender, EventArgs e) 74 { 75 76 } 77 }
以上仅供参考....
为了明天能幸福,今天付出再多也不后悔。