在asp.net页面上得到Castle容器的实例
来源:互联网 发布:2016手机淘宝如何开店 编辑:程序博客网 时间:2024/06/15 08:09
可以如下实现:
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框架也是这样用的,自己就不要这么麻烦了,可以将这个写在页面和控件的基类中。
- 在asp.net页面上得到Castle容器的实例
- 在asp.net页面上得到Castle容器的实例
- ASP.NET 得到页面的源文件
- ASP.NET 得到页面的全部链接!
- Asp.Net中获取Castle容器中的服务的另一方法
- 基于Castle+IBatisNet+Castle.MVC的ASP.NET构架
- 基于Castle+IBatisNet+Castle.MVC的ASP.NET构架
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码
- 开源CMS Alfresco 1.0 发布
- Linux能否靠架构取得胜利
- 编译错误CS1595
- ArgoUML -- 开源UML 建模工具
- Castle.MVC框架介绍
- 在asp.net页面上得到Castle容器的实例
- MySql 字符串函数--
- 实模式内存分布
- MVC结构简介
- Castle 整合.NET Remoting
- 我为何需要使用空接口?
- 用Open XML SDK读取Excel
- NHibernate 1.0 Released 版本发布了
- 使用ASP.NET AJAX让GridView的数据行显示提示框(ToolTip)