应用程序域

一.什么是应用程序域?

应用程序域 (application domain) (AppDomain) 一种边界,它由公共语言运行库围绕同一应用程序范围内创建的对象建立(即,从应用程序入口点开始,沿着对象激活的序列的任何位置)。应用程序域有助于将在一个应用程序中创建的对象与在其他应用程序中创建的对象隔离,以使运行时行为可以预知。在一个单独的进程中可以存在多个应用程序域。
 

二.什么时候用应用程序域?

在一个应用程序中出现的错误不会影响其他应用程序。因为类型安全的代码不会导致内存错误,所以使用应用程序域可以确保在一个域中运行的代码不会影响进程中的其他应用程序。
能够在不停止整个进程的情况下停止单个应用程序。使用应用程序域使您可以卸载在单个应用程序中运行的代码。
注意 不能卸载单个程序集或类型。只能卸载整个域。

三.应用程序域的简单实例?

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)
{ }
}

以上仅供参考....

AppDomain 应用程序域的更多相关文章

  1. C#学习笔记----AppDomain应用程序域

    使用.Net建立的可执行程序*.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.应用程序域是.Net引入的一个新概念,它比进程所占用的资源要少,可以被看做是一个轻量级 ...

  2. C# 通过 AppDomain 应用程序域实现程序集动态卸载或加载

    AppDomain 表示应用程序域,它是一个应用程序在其中执行的独立环境.每个应用程序只有一个主应用程序域,但是一个应用程序可以创建多个子应用程序域. 因此可以通过 AppDomain 创建新的应用程 ...

  3. .Net环境下的缓存技术介绍 (转)

    .Net环境下的缓存技术介绍 (转) 摘要:介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 ...

  4. MVC5知识点记录

    IIS/ASP.NET管道 原理永远是重中之重,所以在开篇的地方,先了解一下地址栏输入网址回车之后的故事. 不同IIS版本处理请求也不一样 IIS5 IIS 5.x 运行在进程InetInfo.exe ...

  5. .NET架构设计、框架设计系列文章总结

    从事.NET开发到现在已经有七个年头了.慢慢的可能会很少写.NET文章了.不知不觉竟然走了这么多年,热爱.NET热爱c#.突然想对这一路的经历进行一个总结. 是时候开始下一阶段的旅途,希望这些文章可以 ...

  6. [转]C#反射-Assembly.Load、LoadFrom与LoadFile进阶

    关于.NET中的反射,常用的有三个方法: Assembly.Load()Assembly.LoadFrom()Assembly.LoadFile() 下面说说这三个方法的区别和一些细节问题 1. As ...

  7. ASP.NET学习链接

    张子阳个人ASP.NET技术博客:http://www.tracefact.net/Asp-Net/ 动态加载asp.net分页控件:http://www.cnblogs.com/cresuccess ...

  8. .Net环境下的缓存技术介绍

    .Net环境下的缓存技术介绍 摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1         概念 1.1 ...

  9. Asp.net管道模型(管线模型)

    Asp.net管道模型(管线模型)   前言 为什么我会起这样的一个标题,其实我原本只想了解asp.net的管道模型而已,但在查看资料的时候遇到不明白的地方又横向地查阅了其他相关的资料,而收获比当初预 ...

随机推荐

  1. 重读《Struts In Action》

    Figure   1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java   platform. S ...

  2. poj 1300 Door Man 欧拉回路

    题目链接:http://poj.org/problem?id=1300 You are a butler in a large mansion. This mansion has so many ro ...

  3. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  4. 【BZOJ】【1177】【APIO2009】Oil

    DP 找出三个正方形,可以转化为将整个油田切成三个矩形块,每块中各找一个正方形区域,切的形式只有6种,分类更新ans即可 题解:http://trinklee.blog.163.com/blog/st ...

  5. NYOJ-517 最小公倍数 TLE 分类: NYOJ 2013-12-29 14:49 253人阅读 评论(0) 收藏

    #include <stdio.h> int main(){ int num[101]={0}; int result[21]={0}; int sum[101][21]={0}; int ...

  6. shader 里面的分支

    shader 里面的真分支会降低效率 一种方法:构造一个分段函数出来 比如saturate(depth*1.5f)

  7. Slim - 超轻量级PHP Restful API构建框架

    下载源码包: http://www.slimframework.com/ 基于Slim的Restful API Sample: <?php require '/darjuan/Slim/Slim ...

  8. mvc从xheditor编辑器中获取内容时存在潜在危险

    xmfdsh在使用xheditor提交要发布的文章等内容的时候出现了如下的错误: 从客户端(Content="<p style="text-align...")中检 ...

  9. .NET设计模式(17):命令模式(Command Pattern)(转)

    概述 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这种情况下,如何将“行为 ...

  10. 定位position详解:relative与absolute

    定位标签:position 包含属性:relative(相对) absolute(绝对) 1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在的位置上.然后通过设 ...