应用程序域
应用程序域
在进行学习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}\r\n", appDomain.Id); //对于每一个web应用程序域,都有一个HostingEnviroment sb.AppendFormat("virtualpath:{0\r\n}", HostingEnvironment.ApplicationVirtualPath); sb.AppendFormat("ApplicationPhysicalPath:{0}\r\n", HostingEnvironment.ApplicationPhysicalPath); return sb.ToString(); } }
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2014-02-16 [c#基础]关于try...catch最常见的笔试题