[Castle]Asp.Net中获取Castle容器中的服务的另一方法

我们知道在我们使用Castle IOC的时候,若你的类库在容器中的时候,你只需要通过公开属性或构造时设置参数后,Castle容器就会自动的根据配置文件中的服务为您实例化相应的类。但他并不支持Asp.Net的后置代码类。那么在Asp.Net中应如何获取容器中的服务呢?
我们可以通过如下方式:
None.gif IWindsorContainer container  =  ContainerAccessorUtil.GetContainer();
None.gifUserManage um 
=  (UserManage)container[ " usermanage " ];
其中 usermanage就是配置文件中的component的ID.
我曾通过这个方式写了一个页面基类,页面继承该类后,就可以通过公开属性的方式来得到服务。
None.gif private  BindingFlags BINDING_FLAGS_SET
None.gif            
=  BindingFlags.Public
None.gif            
|  BindingFlags.SetProperty
None.gif            
|  BindingFlags.Instance
None.gif            
|  BindingFlags.SetField
None.gif            ;
None.gif
None.gif
None.gif        
protected   override   void  OnInit(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//判断退出
InBlock.gif
            if (IsCheckLogin== true && this.UserId == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Redirect(
"~/index.aspx");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IWindsorContainer container 
= ObtainContainer();
InBlock.gif
InBlock.gif            Type type 
= this.GetType();
InBlock.gif
InBlock.gif            PropertyInfo[] properties 
= type.GetProperties(BINDING_FLAGS_SET);
InBlock.gif
InBlock.gif            
foreach (PropertyInfo propertie in properties)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string pname = propertie.Name;
InBlock.gif
InBlock.gif                
if (container.Kernel.HasComponent(pname))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
InBlock.gif                    propertie.SetValue(
this, container[pname], null);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnInit(e);
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif
None.gif        
public  IWindsorContainer ObtainContainer()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif            IContainerAccessor containerAccessor 
=
InBlock.gif
InBlock.gif                 HttpContext.Current.ApplicationInstance 
as IContainerAccessor;
InBlock.gif            
if (containerAccessor == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            IWindsorContainer container 
= containerAccessor.Container;
InBlock.gif            
if (container == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ApplicationException("HttpApplication 得不到容器的实例");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return container;
InBlock.gif
ExpandedBlockEnd.gif        }

你也可以看 自由、创新、研究、探索…… 的博客上也有相应的介绍: 在asp.net页面上得到Castle容器的实例

这是我以前的做法,今天所要讲的是另一种使用方式,通过HttpHandler的方式将每个页面的HttpHandler增加到Castle容器中。(以下代码来自互联网)
None.gif public   class  PageHandler : IHttpHandler, IRequiresSessionState
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static readonly ILog log = LogManager.GetLogger(typeof(PageHandler));
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IHttpHandler Members#region IHttpHandler Members
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Process the aspx request. This means (eventually) rewriting the url and registering the page 
InBlock.gif        
/// in the container.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="context"></param>

InBlock.gif        public void ProcessRequest(HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string rawUrl = context.Request.RawUrl;
InBlock.gif            log.Info(
"Starting request for " + rawUrl);
InBlock.gif            DateTime startTime 
= DateTime.Now;
InBlock.gif
InBlock.gif            
// Rewrite url
InBlock.gif            
//UrlRewriter urlRewriter = new UrlRewriter(context);
InBlock.gif            
//string rewrittenUrl = urlRewriter.RewriteUrl(rawUrl);
InBlock.gif
InBlock.gif            
// Obtain the handler for the current page
InBlock.gif
            string aspxPagePath = rawUrl.Substring(0, rawUrl.IndexOf(".aspx"+ 5);
InBlock.gif            IHttpHandler handler 
= PageParser.GetCompiledPageInstance(aspxPagePath, null, context);
InBlock.gif
InBlock.gif            
// Register the page in the container
InBlock.gif
            handler = RegisterAspxPage(handler, context);
InBlock.gif
InBlock.gif            
// Process the page just like any other aspx page
InBlock.gif
            handler.ProcessRequest(context);
InBlock.gif
InBlock.gif            
// Remove the page from the container
InBlock.gif
            RemoveAspxPage(handler, context);
InBlock.gif
InBlock.gif            
// Log duration
InBlock.gif
            TimeSpan duration = DateTime.Now - startTime;
InBlock.gif            log.Info(String.Format(
"Request finshed. Total duration: {0} ms.", duration.Milliseconds));
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsReusable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn true; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private IHttpHandler RegisterAspxPage(IHttpHandler handler, HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (handler is IRuixinPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string pageKey = Guid.NewGuid().ToString();
InBlock.gif                IWindsorContainer container 
= ContainerAccessorUtil.GetContainer();
InBlock.gif                container.Kernel.AddComponent(pageKey, handler.GetType());
InBlock.gif
InBlock.gif                
if (container.Kernel.HasComponent(handler.GetType()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    IHttpHandler newHandler 
= (IHttpHandler)container[handler.GetType()];
InBlock.gif                    handler 
= newHandler;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return handler;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void RemoveAspxPage(IHttpHandler handler, HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (handler is IRuixinPage)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IWindsorContainer container 
= (context.ApplicationInstance as IContainerAccessor).Container;
InBlock.gif                container.Release(handler);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

这样在我们的后置代码类中就可以通过公开属性的方式来获取容器中的服务。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值