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 应用程序域的更多相关文章
- C#学习笔记----AppDomain应用程序域
使用.Net建立的可执行程序*.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.应用程序域是.Net引入的一个新概念,它比进程所占用的资源要少,可以被看做是一个轻量级 ...
- C# 通过 AppDomain 应用程序域实现程序集动态卸载或加载
AppDomain 表示应用程序域,它是一个应用程序在其中执行的独立环境.每个应用程序只有一个主应用程序域,但是一个应用程序可以创建多个子应用程序域. 因此可以通过 AppDomain 创建新的应用程 ...
- .Net环境下的缓存技术介绍 (转)
.Net环境下的缓存技术介绍 (转) 摘要:介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1 概念 ...
- MVC5知识点记录
IIS/ASP.NET管道 原理永远是重中之重,所以在开篇的地方,先了解一下地址栏输入网址回车之后的故事. 不同IIS版本处理请求也不一样 IIS5 IIS 5.x 运行在进程InetInfo.exe ...
- .NET架构设计、框架设计系列文章总结
从事.NET开发到现在已经有七个年头了.慢慢的可能会很少写.NET文章了.不知不觉竟然走了这么多年,热爱.NET热爱c#.突然想对这一路的经历进行一个总结. 是时候开始下一阶段的旅途,希望这些文章可以 ...
- [转]C#反射-Assembly.Load、LoadFrom与LoadFile进阶
关于.NET中的反射,常用的有三个方法: Assembly.Load()Assembly.LoadFrom()Assembly.LoadFile() 下面说说这三个方法的区别和一些细节问题 1. As ...
- ASP.NET学习链接
张子阳个人ASP.NET技术博客:http://www.tracefact.net/Asp-Net/ 动态加载asp.net分页控件:http://www.cnblogs.com/cresuccess ...
- .Net环境下的缓存技术介绍
.Net环境下的缓存技术介绍 摘要: 介绍缓存的基本概念和常用的缓存技术,给出了各种技术的实现机制的简单介绍和适用范围说明,以及设计缓存方案应该考虑的问题(共17页) 1 概念 1.1 ...
- Asp.net管道模型(管线模型)
Asp.net管道模型(管线模型) 前言 为什么我会起这样的一个标题,其实我原本只想了解asp.net的管道模型而已,但在查看资料的时候遇到不明白的地方又横向地查阅了其他相关的资料,而收获比当初预 ...
随机推荐
- android开发支付宝接口开发流程(密钥篇)
参考博客:http://blog.it985.com/12276.html 官方下载地址:http://download.alipay.com/public/api/base/WS_MOBILE_PA ...
- 6、android 普通日志输出到SD卡
这是本人见过写博文最负责的一个人: http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_ca ...
- 腾讯qq的发展史
腾讯qq的发展史 即时通信软件的历史并不久远,但是它一诞生,就立即受到网民的喜爱,并风靡全球. 在其发展史上,以色列人功不可没.正是四位以色列籍的年轻人,在1996年7月成立的Mirabilis ...
- c++ ip地址的操作 c版
http://blog.csdn.net/cpp_funs/article/details/6988154 1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- 【BZOJ】【3613】【HEOI2014】南园满地堆轻絮
思路题 考试结束前5.6min的时候想到……但是写挂了QAQ 其实就是(差值最大的逆序对之差+1)/2; 找逆序对其实维护一个max直接往过扫就可以了……因为逆序对是前面的数大于后面的数…… 正确性显 ...
- 客户端发包 GS端接收
客户端发包,GS接收 bool GameServer::ProcessLoop(packet& rPkt)//GS线程做的 { if(false == m_spDataLayer->Re ...
- css之margin && padding讲解
margin && padding盒子模型: margin是模块与模块的空隙,padding是内容与边框的空隙 注: 1.margin:边缘.空白 2.padding:填充 margi ...
- HTTP persistent connection
http://en.wikipedia.org/wiki/HTTP_persistent_connection
- ASP.NET防止用户多次登录的方法
常见的处理方法是,在用户登录时,判断此用户是否已经在Application中存在,如果存在就报错,不存在的话就加到Application中(Application是所有Session共有的,整个web ...