温莎城堡和IHttpHandler和IHttpHandlerFactory [英] Castle windsor and IHttpHandler and IHttpHandlerFactory

查看:102
本文介绍了温莎城堡和IHttpHandler和IHttpHandlerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个RIA应用程序,其中客户端上有javascript(我正在使用Ext),服务器上有.NET,对于json-rpc,我正在使用Jayrock,这是一个不错的库(至少对我来说) ),因为它很简单且效果很好,所以我过去曾经使用过它.

I'm developing a RIA application where there is javascript on the client (i'm using Ext) and .NET on the server, for json-rpc I'm using Jayrock which is a nice library (at least for me) as it is simple and works well, Ive used it in the past.

Jayrock使用Web Handlers处理json-rpc请求,您编写一个实现IHttpHandler的类,该类派生自具有某些属性的Jayrock类,其余部分则为浏览器提供了一个javascript类以发挥其魔力.

Jayrock uses Web Handlers to serve the json-rpc request, you code a class that implements IHttpHandler and derives from a Jayrock class with some attributes, and it does the rest to provide a javascript class to the browser to do its magic.

现在,通常Web处理程序将具有无参数的构造函数,但我想在其上使用DI,并使用windsor为我解决依赖项

Now, normally web handlers will have parameter-less constructors, but I want to use DI on them, and use windsor to resolve the dependencies for me

所以,我将有一些类似于以下的课程

So, I will have some class like the following

    public class VistaEntidadSimpleServer :  JsonRpcVistaHandler ,IHttpHandler
{
    public VistaEntidadSimpleServer(ISomeDependecy someObject)
    {
                 someObject.doSomething();

    }


    [JsonRpcMethod("Aceptar")]
    public string Aceptar (IVista vista)
    {
        throw new NotImplementedException ();
    }


    [JsonRpcMethod("Cancelar")]
    public string Cancelar (IVista vista)
    {
        throw new NotImplementedException ();
    }


    public IVista CargarDatos(IVista vista)
    {
        throw new System.NotImplementedException();
    }

}

所以,现在的问题是如何使温莎处于中间位置进行解决.在四处搜寻之后,从看来春天可以做的事情来看,我想我可以尝试一下IHttpHandlerFactory并编写类似这样的代码

So, now the problem is how to get windsor in the middle to do the resolving. After searching around, and from what it seems to do spring, I think I can give a try to IHttpHandlerFactory, and code something like this

    public class CastleWindsorHttpHandlerFactory : IHttpHandlerFactory
{
    public CastleWindsorHttpHandlerFactory ()
    {
        if (container==null)
        container=(IWindsorContainer)HttpRuntime.Cache.Get("Container");
    }

    #region IHttpHandlerFactory implementation

    public IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated)
    {
        return container.Resolve<IHttpHandler>(url);
    }

    public void ReleaseHandler (IHttpHandler handler)
    {
        container.Release(handler);
    }

    #endregion

    private static IWindsorContainer container=null;
}

配置Web应用程序以将工厂用于ashx文件,并在global.asax中创建容器,使用url作为id配置处理程序,并将包含器注册到Web缓存中.

Configuring the web app to use the factory for ashx files, and creating the container in global.asax, configuring the handlers with the url as ids, and registering the containter into the web cache.

您认为这是一个不错的解决方案吗?或者我在这里缺少什么,还有另一种方法可以使容器处理Web处理程序吗?

Do you think this is a nice solution?, or is there something I am missing here, is there another way to get web handlers resolved by the container?

感谢进阶

推荐答案

与其将容器存储在缓存中,还请在全局HttpApplication中实现IContainerAccessor来引用您的容器.无需在CastleWindsorHttpHandlerFactory中存储引用.

Instead of storing the container in cache, implement IContainerAccessor in your global HttpApplication to reference your container. No need to store a reference in CastleWindsorHttpHandlerFactory.

除此之外,它看起来还不错.

Other than that, it looks good.

这篇关于温莎城堡和IHttpHandler和IHttpHandlerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆