在asp.net页面上得到Castle容器的实例

简介:
在项目中使用 Castle IOC 容器, Asp.net 程序中如何得到 Castle 容器内。
可以如下实现:
1 Gloabal 实现接口 IContainerAccessor
public  class Global : System.Web.HttpApplication, IContainerAccessor
      {
         /// <summary>
         /// 必需的设计器变量。
         /// </summary>
          private System.ComponentModel.IContainer components = null;
          private static WindsorContainer container;
          
         public Global()
         {
              InitializeComponent();
         }   
          protected void Application_Start(Object sender, EventArgs e)
         {
              container = new MyContainer(new XmlInterpreter("app_config.xml"));
         }  
          protected void Session_Start(Object sender, EventArgs e)
         {
         }
……
    protected void Application_End(Object sender, EventArgs e)
    {
              container.Dispose();
    }  
          #region  IContainerAccessor implementation
         public IWindsorContainer Container
         {
              get { return container; }
         }
              
}
     2、使用如下类来获取容器实例,这段代码来自Castle.MVC
     ///   <summary>
     /// Uses the HttpContext and the <see cref="IContainerAccessor"/> 
     /// to access the container instance.
     /// </summary>
     public abstract class ContainerWebAccessorUtil
     {
         /// <summary>
         /// 从application中获取一个容器实例
         /// </summary>
         /// <returns>返回一个 IWindsorContainer</returns>
         public static IWindsorContainer ObtainContainer()
         {
              IContainerAccessor containerAccessor =
                   HttpContext.Current.ApplicationInstance as IContainerAccessor;  
              if (containerAccessor == null)
              {
                   throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性 );
              }    
              IWindsorContainer container = containerAccessor.Container;  
              if (container == null)
              {
                   throw new ApplicationException("HttpApplication 得不到容器的实例");
              }
              return container;
         }
     }  
3、在具体的用户控件,页面的后置代码中通过ContainerWebAccessorUtil. ObtainContainer()获取容器实例。当然如果使用Castle.MVC,MVC框架也是这样用的,自己就不要这么麻烦了,可以将这个写在页面和控件的基类中。





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/75210,如需转载请自行联系原作者
目录
相关文章
|
程序员 数据处理 数据安全/隐私保护
Harmony 个人中心(页面交互、跳转、导航、容器组件)(上)
Harmony 个人中心(页面交互、跳转、导航、容器组件)(上)
358 0
|
容器
.net core Autofac IOC 容器的简单使用
## 书接上回,介绍了[.net core 读取配置文件的几种方式](https://developer.aliyun.com/article/1363340?spm=a2c6h.13148508.setting.14.21764f0ehMR1KI ".net core 读取配置文件的几种方式"),本文学习Autofac的同时再次增加一种读取配置文件的方法。 ## 本文介绍Auofac,一个优秀的.NET IOC框架 ## 源码地址:https://github.com/autofac/Autofac # 1、打开NuGet包管理器安装Autofac.Extensions.Dependenc
281 0
|
容器
.NET Core - Autofac增强容器能力
.NET Core - Autofac增强容器能力
|
API 索引 容器
Harmony 个人中心(页面交互、跳转、导航、容器组件)(下)
Harmony 个人中心(页面交互、跳转、导航、容器组件)(下)
597 0
|
开发框架 JavaScript 前端开发
揭秘:如何让你的asp.net页面变身交互魔术师——先施展JavaScript咒语,再引发服务器端魔法!
【8月更文挑战第16天】在ASP.NET开发中,处理客户端与服务器交互时,常需先执行客户端验证再提交数据。传统上使用ASP.NET Button控件直接触发服务器事件,但难以插入客户端逻辑。本文对比此法与改进方案:利用HTML按钮及JavaScript手动控制表单提交。后者通过`onclick`事件调用JavaScript函数`SubmitForm()`来检查输入并决定是否提交,增强了灵活性和用户体验,同时确保了服务器端逻辑的执行。
217 5
|
开发框架 .NET 中间件
七天.NET 8操作SQLite入门到实战 - (2)第七天Blazor班级管理页面编写和接口对接
七天.NET 8操作SQLite入门到实战 - (2)第七天Blazor班级管理页面编写和接口对接
404 7
|
开发框架 前端开发 .NET
七天.NET 8操作SQLite入门到实战 - (3)第七天Blazor学生管理页面编写和接口对接
七天.NET 8操作SQLite入门到实战 - (3)第七天Blazor学生管理页面编写和接口对接
255 1
|
Linux Docker 容器
蓝易云 - net.ipv4.ip_forward=0导致docker容器无法与外部通信
完成以上步骤后,Docker容器应该能够正常与外部通信了。
858 2
Could not autowire No beans of ‘UserSerice,这样的bug,主要是idea检测到你没有往页面中,没有往容器中注入一个userService,容器注入UserSe
Could not autowire No beans of ‘UserSerice,这样的bug,主要是idea检测到你没有往页面中,没有往容器中注入一个userService,容器注入UserSe