Castle-GlobalApplication.cs工程起始加载

本文介绍了一个ASP.NET应用程序如何在启动时进行初始化,并通过Castle Windsor实现了依赖注入。此外,还展示了如何在启动时加载缓存数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//是用在工程前加载的类型

namespace SystemManager
{
 using System;
 using System.Web;
 using System.Web.Caching;
 using Castle.Windsor;
 using Castle.Windsor.Configuration.Interpreters;
 using SystemManager.Service;

 public class GlobalApplication : HttpApplication, IContainerAccessor
 {
  private static IWindsorContainer container;

  public GlobalApplication()
  {
  }

  #region IContainerAccessor

  public IWindsorContainer Container
  {
   get { return container; }
  }

  #endregion

//OnStart在程序加载时调用

  public void Application_OnStart()
  {
   container = new WindsorContainer(new XmlInterpreter());
   container.AddComponent("Pager", typeof(SystemManager.Components.Pager));
   container.AddComponent("AjaxPager", typeof(SystemManager.Components.AjaxPager));
   container.AddComponent("NewPager", typeof(SystemManager.Components.NewPager));
      //在工程启动后加载的Cache内存中数据。
   CardStatisticsService _cardStatisticsService = container[typeof(CardStatisticsService)] as CardStatisticsService;
   HttpContext.Current.Cache["category1"] = _cardStatisticsService.GetCategory(1);
   HttpContext.Current.Cache["category2"] = _cardStatisticsService.GetCategory(2);
   HttpContext.Current.Cache["category3"] = _cardStatisticsService.GetCategory(3);


  }

  public void Application_OnEnd()
  {
   container.Dispose();
  }

  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;
  }
 }
}

转载于:https://www.cnblogs.com/zengxlf/archive/2009/05/06/1451040.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值