AppDomain 类


表示应用程序域,它是一个应用程序在其中执行的独立环境。 此类不能被继承。


命名空间:   System

程序集:  mscorlib(位于 mscorlib.dll)

 ​

    System.AppDomain

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class AppDomain : MarshalByRefObject, _AppDomain,
IEvidenceFactory

 

    备注    

应用程序域,由表示AppDomain对象,帮助提供有关执行托管的代码的隔离、 卸载和安全边界。



使用应用程序域隔离可能会终止进程的任务。 如果状态AppDomain,正在执行的任务变得不稳定,AppDomain可以而不会影响进程中卸载。 进程必须运行很长一段无需重新启动时,这很重要。 你还可以使用应用程序域隔离不应共享数据的任务。



如果程序集被加载到默认应用程序域,它无法从内存中卸载过程运行时。 但是,如果您打开第二个应用程序域加载和执行程序集,程序集是卸载卸载该应用程序域时。 使用此方法最大程度减少偶尔使用大型 Dll 的长时间运行进程的工作集。



多个应用程序域可以运行在一个进程中;但是,没有应用程序域和线程之间的一对一的相关性。 多个线程可以属于单个应用程序域,并且单个应用程序域中时的给定的线程并不局限于单个应用程序域,在任何给定时间,执行线程。

使用创建应用程序域CreateDomain方法。 AppDomain实例用于加载和执行程序集 (Assembly)。 当AppDomain是不再在使用中,可以将它卸载。

AppDomain类实现的一组启用应用程序进行响应时加载的程序集,应用程序域将被卸载,或引发未经处理的异常时的事件。

有关使用应用程序域的详细信息,请参阅应用程序域。

此类实现MarshalByRefObject, _AppDomain,和IEvidenceFactory接口。

决不要创建的远程操作包装AppDomain对象。 这样可以将发布到的远程引用AppDomain,如公开方法CreateInstance与远程访问和有效地销毁该的代码访问安全性AppDomain。 恶意客户端连接到远程AppDomain无法获取任何资源的访问权限AppDomain本身具有访问权限。 不创建任何扩展的类型的远程操作包装MarshalByRefObject并实现恶意客户端无法用于绕过安全系统的方法。


默认值为AppDomainSetup.DisallowCodeDownload属性是false。 此设置是不安全的服务。 若要防止服务下载部分受信任的代码,请将此属性设置为true


示例    

此示例演示如何创建一个新AppDomain,新实例化中的一个类型AppDomain,以及与该类型的对象通信。 此外,此示例演示如何卸载AppDomain导致要进行垃圾回收的对象。

using System;
using System.Reflection;
using System.Threading;

class Module1
{
public static void Main()
{
// Get and display the friendly name of the default AppDomain.
string callingDomainName = Thread.GetDomain().FriendlyName;
Console.WriteLine(callingDomainName);

// Get and display the full name of the EXE assembly.
string exeAssembly = Assembly.GetEntryAssembly().FullName;
Console.WriteLine(exeAssembly);

// Construct and initialize settings for a second AppDomain.
AppDomainSetup ads = new AppDomainSetup();
ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;

ads.DisallowBindingRedirects = false;
ads.DisallowCodeDownload = true;
ads.ConfigurationFile =
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

// Create the second AppDomain.
AppDomain ad2 = AppDomain.CreateDomain("AD #2", null, ads);

// Create an instance of MarshalbyRefType in the second AppDomain.
// A proxy to the object is returned.
MarshalByRefType mbrt =
(MarshalByRefType) ad2.CreateInstanceAndUnwrap(
exeAssembly,
typeof(MarshalByRefType).FullName
);

// Call a method on the object via the proxy, passing the
// default AppDomain's friendly name in as a parameter.
mbrt.SomeMethod(callingDomainName);

// Unload the second AppDomain. This deletes its object and
// invalidates the proxy object.
AppDomain.Unload(ad2);
try
{
// Call the method again. Note that this time it fails
// because the second AppDomain was unloaded.
mbrt.SomeMethod(callingDomainName);
Console.WriteLine("Sucessful call.");
}
catch(AppDomainUnloadedException)
{
Console.WriteLine("Failed call; this is expected.");
}
}
}

// Because this class is derived from MarshalByRefObject, a proxy
// to a MarshalByRefType object can be returned across an AppDomain
// boundary.
public class MarshalByRefType : MarshalByRefObject
{
// Call this method via a proxy.
public void SomeMethod(string callingDomainName)
{
// Get this AppDomain's settings and display some of them.
AppDomainSetup ads = AppDomain.CurrentDomain.SetupInformation;
Console.WriteLine("AppName={0}, AppBase={1}, ConfigFile={2}",
ads.ApplicationName,
ads.ApplicationBase,
ads.ConfigurationFile
);

// Display the name of the calling AppDomain and the name
// of the second domain.
// NOTE: The application's thread has transitioned between
// AppDomains.
Console.WriteLine("Calling from '{0}' to '{1}'.",
callingDomainName,
Thread.GetDomain().FriendlyName
);
}
}

/* This code produces output similar to the following:

AppDomainX.exe
AppDomainX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
AppName=, AppBase=C:\AppDomain\bin, ConfigFile=C:\AppDomain\bin\AppDomainX.exe.config
Calling from 'AppDomainX.exe' to 'AD #2'.
Failed call; this is expected.
*/




此类型的所有公共静态(Visual Basic 中的 已共享 在 Visual Basic 中)成员都是线程安全的。不保证所有实例成员都是线程安全的。