温莎城堡和 IHttpHandler 和 IHttpHandlerFactory
我正在开发一个 RIA 应用程序,其中客户端上有 javascript(我使用的是 Ext),服务器上有 .NET,对于 json-rpc,我使用的是 Jayrock,这是一个很好的库(至少对我来说),因为它很简单,效果也很好,我以前用过。
Jayrock 使用 Web 处理程序来服务 json-rpc 请求,您编写一个实现 IHttpHandler 的类,并从具有一些属性的 Jayrock 类派生,它会完成剩下的工作,以向浏览器提供一个 javascript 类来发挥其魔力。
现在,通常Web处理程序将具有无参数构造函数,但我想在它们上使用DI,并使用windsor来解决我的依赖关系
所以,我将有一些像下面这样的类
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中间做解析。经过搜索后,从 Spring 似乎所做的事情来看,我想我可以尝试 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 处理程序?
提前致谢
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 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.
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();
}
}
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;
}
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.
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?
Thanks in advancce
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将容器存储在缓存中,而是在全局 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 inCastleWindsorHttpHandlerFactory
.Other than that, it looks good.