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的管道模型而已,但在查看资料的时候遇到不明白的地方又横向地查阅了其他相关的资料,而收获比当初预 ...
随机推荐
- jQuery Dialog弹出层对话框插件
Dialog.js的相关注释已经添加,可以按照注释,进行相关样式的修改,适用于自定义的各个系统! dialog.js /** * jQuery的Dialog插件. * * @param object ...
- 《IT小小鸟》阅读心得
我是一个不爱看书的人,认为看那些又臭又长废话连篇的书是在浪费时间,但我不认为在那么多的书中没有好书,在一次的职业生涯规划中老师推荐了这本书,一开始认为不值得一看,但还是拿了起来,读了有不少的感触. 书 ...
- 使用Fiddler
右键一个Result,点击Inspect in new Window(Shift+Enter)
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
- 浏览器执行js
Scriptish chrome自带 greasemonkey http://www.firefox.net.cn/forum/viewtopic.php?f=5&t=45715
- post 方式提交XML文件调用接口
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java. ...
- iOS-OpenGLES 简单渲染
UIImage *showImage = [UIImage imageNamed:, , showImage.size.width, showImage.size.height); ) fo ...
- 社交APP经典死法18种,听野路子产品菜狗怎么说
点这里 社交APP经典死法18种,听野路子产品菜狗怎么说 时间 2015-04-06 11:24:53 虎嗅网相似文章 (4)原文 http://www.huxiu.com/article/112 ...
- css系列-段落首字符下沉、缩进及特殊显示
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- UML类图、接口、包、关系
一.类图:允许我们去标记静态内容及类之间的关系. 类的基本表示法: 名称 属性(类型,可见性) 方法(参数,返回值) tip: 显示可见性:Options->Show Visibility 显 ...