.Net中的AppDomain的意思是什么?

paradise885 2007-05-18 03:08:46
.Net中的AppDomain的意思是什么?msdn中就一句话说明,但是对此不了解,请高手详细解释下
...全文
1497 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinahack521 2011-12-14
  • 打赏
  • 举报
回复
应用程序域。
就是在一个进程中 可以运行多个AppDomain 。
可以把AppDomain理解成 程序集(Assembly)的逻辑容器,他是装程序集的 就是DLL EXE 的。看不见

主要是为了安全考虑。


两个AppDomain 可以通信:
一般通过“传值封送” 回传的那个类的实例必须 可序列化 不然能封送;
传“传引用封送” 效率很低。 传引用封送 是继承了MarshalByRefObject 这个类才能传引用封送。。。。
nairenxunwei 2007-11-28
  • 打赏
  • 举报
回复
.net 的一个基本常识, 1个进程下面有若干个appdomain,每个appdomain下面有若干个assembly, 之所以有appdomain的感念, 是因为appdomain为了把程序在内存中的分配孤立起来,其主要目的就是为了安全考虑, 当跨appdomain请求assembly的时候, .net有很细力度的security 和 permission的控制, 推荐看看 1本书 叫<<programming .net security>> ,这样, 你就可以清楚了
wycg_cnh20 2007-11-28
  • 打赏
  • 举报
回复
顶!
屁屁 2007-05-25
  • 打赏
  • 举报
回复
这个东西在实际开发系统中有什么用处呢?
北京的雾霾天 2007-05-21
  • 打赏
  • 举报
回复
.NET Framework 类库
AppDomain 类
表示应用程序域,它是一个应用程序在其中执行的独立环境。无法继承此类。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)
应用程序域(由 AppDomain 对象表示)为执行托管代码提供隔离、卸载和安全边界。

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

如果程序集被加载到默认应用程序域中,则当进程运行时将无法从内存中卸载该程序集。但是,如果打开另一个应用程序域来加载和执行程序集,则卸载该应用程序域时也会同时卸载程序集。使用此技术最小化长时间运行的进程的工作集,这些进程偶尔会使用大型 DLL。

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

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

AppDomain 类实现一组事件,这些事件使应用程序可以在加载程序集、要卸载应用程序域或引发未处理的异常时进行响应。

有关使用应用程序域的更多信息,请参见应用程序域。

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

在任何情况下都不应创建 AppDomain 对象的可远程控制的包装。这样做可发布对该 AppDomain 的远程引用,将诸如 CreateInstance 方法向远程访问公开,并有效损坏该 AppDomain 的代码访问安全性。连接到远程 AppDomain 的恶意客户端可以获得对 AppDomain 本身可访问的所有资源的访问权。您不应为任何以下类型创建可远程控制的包装:扩展 MarshalByRefObject 的类型和实现恶意客户端可用来绕过安全系统的方法的类型。

此示例显示如何创建新的 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 =
"file:///" + System.Environment.CurrentDirectory;
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.
*/
paradise885 2007-05-21
  • 打赏
  • 举报
回复
有没有更详细的资料看看

谢谢!
shuo_shu 2007-05-18
  • 打赏
  • 举报
回复
应用程序域. 逻辑上的一个概念.

指程序运行后所有资源的一个集合

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧