博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET加载应用程序域
阅读量:5310 次
发布时间:2019-06-14

本文共 2974 字,大约阅读时间需要 9 分钟。

使用GAC和bin加载WEB应用程序域,被加载的对象必须继承MarshalByRefObject

代码如下:

public class Intelligencer : MarshalByRefObject    {        public string Report()        {            AppDomain appDomain = AppDomain.CurrentDomain;            StringBuilder sb = new StringBuilder();            sb.AppendFormat("Domain ID:{0}\r\n",appDomain.Id);            sb.AppendFormat("VirtualPath:{0}\r\n",HostingEnvironment.ApplicationVirtualPath);            sb.AppendFormat("PhysicalOath:{0}\r\n", HostingEnvironment.ApplicationPhysicalPath);            return sb.ToString();        }            }
static void TestHosting()             {                Intelligencer intell = ApplicationHost.CreateApplicationHost(typeof(Intelligencer), "/", Environment.CurrentDirectory) as Intelligencer;                 Console.WriteLine("Current Domain ID:{0}\r\n",AppDomain.CurrentDomain.Id);                 Console.WriteLine(intell.Report());             }

如果调用的对象继承MarshalByRefObject同时也实现IRegisteredObject  我们可以用ApplicationManager来加载应用程序域

public class Intelligencer : MarshalByRefObject, IRegisteredObject    {        public string Report()        {            AppDomain appDomain = AppDomain.CurrentDomain;            StringBuilder sb = new StringBuilder();            sb.AppendFormat("Domain ID:{0}\r\n",appDomain.Id);            sb.AppendFormat("VirtualPath:{0}\r\n",HostingEnvironment.ApplicationVirtualPath);            sb.AppendFormat("PhysicalOath:{0}\r\n", HostingEnvironment.ApplicationPhysicalPath);            return sb.ToString();        }        public void Stop(bool immediate)        {            HostingEnvironment.UnregisterObject(this);        }    }

调用方式

static void TestHosting()        {            Intelligencer intell = CreateWorkerAppDomainWithHost(typeof(Intelligencer), "/", Environment.CurrentDirectory) as Intelligencer;            Console.WriteLine("Current Domain ID:{0}\r\n",AppDomain.CurrentDomain.Id);            Console.WriteLine(intell.Report());        }        static object CreateWorkerAppDomainWithHost(Type hostType,string virtualPath, string physicalPath)        {           string uniqueAppString = string.Concat(virtualPath, physicalPath).ToLowerInvariant();            string appid = (uniqueAppString.GetHashCode()).ToString("X", CultureInfo.InvariantCulture);            var appmanager=ApplicationManager.GetApplicationManager();            var buildManagerHostType = typeof(HttpRuntime).Assembly.GetType("System.Web.Compilation.BuildManagerHost");            var buildManagerHost = appmanager.CreateObject(appid, buildManagerHostType, virtualPath, physicalPath, false);             buildManagerHostType.InvokeMember("RegisterAssembly", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, buildManagerHost, new object[] {hostType.Assembly.FullName,hostType.Assembly.Location });            return appmanager.CreateObject(appid,hostType,virtualPath,physicalPath,false);        }

转载于:https://www.cnblogs.com/majiang/archive/2012/07/07/2580790.html

你可能感兴趣的文章
简单【用户输入验证】
查看>>
python tkinter GUI绘制,以及点击更新显示图片
查看>>
CS0103: The name ‘Scripts’ does not exist in the current context解决方法
查看>>
20130330java基础学习笔记-语句_for循环嵌套练习2
查看>>
Spring面试题
查看>>
窥视SP2010--第一章节--SP2010开发者路线图
查看>>
C语言栈的实现
查看>>
代码为什么需要重构
查看>>
TC SRM 593 DIV1 250
查看>>
SRM 628 DIV2
查看>>
2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用
查看>>
Python-S9-Day127-Scrapy爬虫框架2
查看>>
SecureCRT的使用方法和技巧(详细使用教程)
查看>>
右侧导航栏(动态添加数据到list)
查看>>
81、iOS本地推送与远程推送详解
查看>>
虚拟DOM
查看>>
自建数据源(RSO2)、及数据源增强
查看>>
关于View控件中的Context选择
查看>>
2018icpc徐州OnlineA Hard to prepare
查看>>
Spark的启动进程详解
查看>>