应用程序域
一.什么是应用程序域?
二.什么时候用应用程序域?
在一个应用程序中出现的错误不会影响其他应用程序。因为类型安全的代码不会导致内存错误,所以使用应用程序域可以确保在一个域中运行的代码不会影响进程中的其他应用程序。
能够在不停止整个进程的情况下停止单个应用程序。使用应用程序域使您可以卸载在单个应用程序中运行的代码。
注意 不能卸载单个程序集或类型。只能卸载整个域。
三.应用程序域的简单实例?
SetupInfo设置程序域的信息
[Serializable]
public class SetupInfo : MarshalByRefObject
{
/// <summary>应用程序域id</summary>
public string Id { get; set; } /// <summary>应用目录</summary>
public string BinaryDirectory { get; set; } /// <summary>应用父目录</summary>
public string BaseDirectory { get; set; } /// <summary>应用入点</summary>
public string EntryPoint { get; set; }
}
ProxyObject代理对象
[Serializable]
public class ProxyObject : MarshalByRefObject
{
public Assembly assembly; public object instance; public Type currentType; public void Initialize()
{
var setupInfo = AppDomain.CurrentDomain.GetData("SETUPINFO") as SetupInfo; string entryClass = "ZLP.AWSW.Person";
string assemblyName = "ZLP.AWSW" + ".dll";
string assemblyPath = Path.Combine(setupInfo.BinaryDirectory, assemblyName);
if (!File.Exists(assemblyPath))
{
return;
}
assembly = Assembly.LoadFrom(assemblyPath);
instance = assembly.CreateInstance(entryClass);
currentType = instance.GetType();
var methinfo = currentType.GetMethod("Execute");
methinfo.Invoke(instance, null);
}
}
Proxy代理
[Serializable]
public class Proxy : MarshalByRefObject
{
public Proxy(SetupInfo setupInfo)
{
this.SetupInfo = setupInfo;
this.Id = setupInfo.Id;
} public string Id { get; set; } public AppDomain Domain { get; set; } public ProxyObject ProxyObject { get; set; } public SetupInfo SetupInfo { get; set; } public void Start()
{
if (Domain == null)
{
Domain = CreateDomain();
}
if (ProxyObject == null)
{
ProxyObject = CreateProxyObject();
}
} public AppDomain CreateDomain()
{
var setup = new AppDomainSetup(); var cachePath = AppDomain.CurrentDomain.SetupInformation.CachePath;
var configurationFile = Path.Combine(SetupInfo.BinaryDirectory, "app.config");
setup.ApplicationBase = SetupInfo.BaseDirectory;
setup.PrivateBinPath = SetupInfo.BinaryDirectory;
setup.ShadowCopyFiles = "true";
setup.ApplicationName = SetupInfo.Id;
setup.ShadowCopyDirectories = string.Format("{0};{1}", SetupInfo.BaseDirectory, SetupInfo.BinaryDirectory);
setup.CachePath = cachePath;
setup.LoaderOptimization = LoaderOptimization.MultiDomainHost;
if (File.Exists(configurationFile))
{
setup.ConfigurationFile = configurationFile;
}
AppDomain domain = AppDomain.CreateDomain(setup.ApplicationName, AppDomain.CurrentDomain.Evidence, setup);
domain.DoCallBack(new CrossAppDomainDelegate(delegate
{
LifetimeServices.LeaseTime = TimeSpan.Zero;
}));
domain.SetData("SETUPINFO", SetupInfo);
domain.DomainUnload += new EventHandler(DomainUnload);
domain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
return domain;
} public ProxyObject CreateProxyObject()
{
Type type = typeof(ProxyObject);
Assembly currentAssembly = Assembly.GetExecutingAssembly();
ProxyObject proxyObject = Domain.CreateInstanceAndUnwrap(currentAssembly.FullName, type.FullName) as ProxyObject;
currentAssembly = null;
type = null;
return proxyObject;
} private void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{ } private void DomainUnload(object sender, EventArgs e)
{ }
}
以上仅供参考....