• 应用程序域


    应用程序域

    在进行学习asp.net内容之前,需要了解一下应用程序域的相关内容。处理asp.net所涉及的类大多数定义在System.Web程序集中。在.Net中,管理程序集的最小逻辑单元为应用程序域(AppDomain)。对于.Net程序来说可以动态的加载程序集到应用程序域中。但是,加载之后的程序集不能单独卸载,只能以应用程序域为单位来整体卸载。

    应用程序域四个机制:

    隔离:不同应用程序域之间不能直接访问。跨应用程序域访问的对象必须派生自System.MarshalByRefObject。

    卸载:被加载的程序集只能以应用程序域为单位卸载。

    安全:以应用程序域为边界的安全机制。

    配置:以应用程序域为边界的程序配置。

    asp.net将网站应用程序寄宿在一个独立的应用程序域中,以便管理。虽然可以通过System.AppDomain也可创建自定义的应用程序域,但,asp.net在System.Web.Hosting命名空间中定义了更加方便的辅助类,以协助程序员寄宿web服务器程序所涉及的应用程序域,并设置应用程序域的相关参数。

    由于我们的应用程序域将于web应用程序运行在不同的应用程序域中,这就涉及到了跨域访问问题。在.Net中,跨域访问的类必需派生自System.MarshalByRefObject。通过这种方式,我们可以得到一个远程对象的代理对象,通过该对象访问位于web应用程序域中的对象。

    web应用程序域

     ApplicationHost.CreateApplicationHost静态方法可以很方便的创建web应用程序所需的应用程序域,并设置所需要的参数。

    namespace System.Web.Hosting
    {
        // Summary:
        //     Enables hosting of ASP.NET pages outside the Internet Information Services
        //     (IIS) application. This class enables the host to create application domains
        //     for processing ASP.NET requests.
        public sealed class ApplicationHost
        {
            // Summary:
            //     Creates and configures an application domain for hosting ASP.NET.
            //
            // Parameters:
            //   hostType:
            //     The name of a user-supplied class to be created in the new application domain.
            //
            //   virtualDir:
            //     The virtual directory for the application domain; for example, /myapp.
            //
            //   physicalDir:
            //     The physical directory for the application domain where ASP.NET pages are
            //     located; for example, c:mypages.
            //
            // Returns:
            //     An instance of a user-supplied class used to marshal calls into the newly
            //     created application domain.
            //
            // Exceptions:
            //   System.PlatformNotSupportedException:
            //     The Web host computer is not running the Windows NT platform or a Coriolis
            //     environment.
            public static object CreateApplicationHost(Type hostType, string virtualDir, string physicalDir);
        }
    }

    hostType:表示用来跨域访问的通信对象,它必须派生自System.MarshalByRefObject。

    virtualDir:表示网站应用程序的根所对应的虚拟目录。

    physicalDir:表示网站应用程序所在的文件系统的文件目录。

    注意

    这个方法需要创建一个新的应用程序域,这个应用程序域将重新加载hostType。并按照以下顺序寻找定义hostType类型的程序集:

    1.GAC

    2.网站物理文件目录下的bin文件夹。

    所以,如果没有将定义hostType的程序集经过数字签名,并加载到GAC中,那么,必需在网站应用程序所在的文件夹下创建bin文件夹,然后将定义hostType的程序集复制到bin文件夹中。

      System.Type hostType = typeof(Intelligencer);
                Intelligencer intelligencer = System.Web.Hosting.ApplicationHost.CreateApplicationHost(hostType, "/", System.Environment.CurrentDirectory) as Intelligencer;
                Console.WriteLine("current domain id:{0}",AppDomain.CurrentDomain.Id);
                Console.WriteLine(intelligencer.Report());
      public class Intelligencer : System.MarshalByRefObject
        {
            public string Report()
            {
                System.AppDomain appDomain = System.AppDomain.CurrentDomain;
                StringBuilder sb = new StringBuilder();
                //应用程序域的信息
                sb.AppendFormat("Domain id:{0}
    ", appDomain.Id);
                //对于每一个web应用程序域,都有一个HostingEnviroment
                sb.AppendFormat("virtualpath:{0
    }", HostingEnvironment.ApplicationVirtualPath);
                sb.AppendFormat("ApplicationPhysicalPath:{0}
    ", HostingEnvironment.ApplicationPhysicalPath);
                return sb.ToString();
            }
        }
  • 相关阅读:
    最小二乘法(转载)
    负反馈放大器电路(转载)
    串联谐振与并联谐振的区别_串联谐振与并联谐振产生谐振的条件(转载)
    运算放大器:虚短与虚断(转载)
    人物根据镜头方向旋转和移动
    浅析勒贝格积分的思想在数论函数求和中的应用
    一类巧妙利用利用分治的序列求值
    YAOI Round #1 (Div.2) 题解
    NOIP 2020 游记
    P4550 收集邮票 与 灵异的期望
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/5193734.html
Copyright © 2020-2023  润新知