zoukankan      html  css  js  c++  java
  • ASP.NET加载应用程序域

    使用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);
            }
  • 相关阅读:
    Luogu1309 瑞士轮(分治,归并排序)
    HYSBZ(BZOJ) 4300 绝世好题(位运算,递推)
    Luogu 1220 关路灯(动态规划)
    HDU 2087 剪花布条(字符串匹配,KMP)
    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
    HDU 1711 Number Sequence (字符串匹配,KMP算法)
    Luogu 3375 【模板】KMP字符串匹配(KMP算法)
    KMP算法(研究总结,字符串)
    CJOJ 1331 【HNOI2011】数学作业 / Luogu 3216 【HNOI2011】数学作业 / HYSBZ 2326 数学作业(递推,矩阵)
    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)
  • 原文地址:https://www.cnblogs.com/majiang/p/2580790.html
Copyright © 2011-2022 走看看