2011-03-21 92 views
2

我正在开发一个RIA应用程序,其中有客户端上的JavaScript(我使用分机)和服务器上的.NET,对于json-rpc我使用Jayrock这是一个不错的图书馆(至少对我来说),因为它很简单,运作良好,我过去曾使用它。城堡windsor和IHttpHandler和IHttpHandlerFactory

Jayrock使用Web Handlers来处理json-rpc请求,你编写一个实现IHttpHandler的类,并从具有某些属性的Jayrock类派生出来,剩下的工作就是为浏览器提供一个JavaScript类来完成它的魔法。

现在,一般的网络处理器将具有无参数的构造函数,但我想用DI他们,并使用温莎解决依赖我

所以,我会有一些类像下面

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

} 

所以,现在的问题是如何让Windsor在中间做解决。摸索,并从它似乎做春天之后,我想我可以给一个尝试IHttpHandlerFactory和代码像这样

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缓存中。

您认为这是一个不错的解决方案吗?或者是有什么我在这里失踪,有没有另一种方法来获得容器解决Web处理程序?在advancce

回答

2

而不是存储在缓存中的容器

感谢,在全局HttpApplication的实施IContainerAccessor引用您的容器。无需在CastleWindsorHttpHandlerFactory中存储参考。

除此之外,它看起来不错。

+0

我在遇到您的建议时遇到问题。我不知道如何从IHttpHandlerFactory的ReleaseHandler方法访问容器。在GetHandler方法中,您获得了作为参数传入的HttpContext的引用(并因此引用了HttpApplication/IcontainerAccessor实例),但ReleaseHandler方法中不是这种情况。当我尝试使用ReleaseHandler中的HttpContext.Current时,WebApp只是静静地死去。关于如何在ReleaseHandler方法中获取IContainerAccessor,您有任何建议吗? – JohannesH 2013-02-21 07:46:41

+0

@JohannesH创建一个新问题或在https://groups.google.com/forum/#!forum/castle-project-users – 2013-02-21 13:30:09

+0

发布好吧,我会的。虽然我会说我通过将容器存储在GetHandler方法的本地字段中来解决此问题。然而,它似乎有点脆弱,因为我不确定这些方法总是被连续调用......它并没有真正的记录。 – JohannesH 2013-02-22 16:54:16