解决HttpControllerContext与温莎城堡 [英] Resolving HttpControllerContext with Castle Windsor

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

问题描述

的ASP.NET Web API ,HttpControllerContext实例提供了很多关于当前环境信息,包括URI当前请求。

In the ASP.NET Web API, HttpControllerContext instances provide a lot of information about the current environment, including the URI of the current request.

如果一个服务依赖于这样的信息(例如,请求URI)时,它应该可以注入该信息到服务

If a service relies on such information (e.g. the request URI), it should be possible to inject that information into the service.

这是pretty容易使用穷人的DI做:只是<一个href=\"http://blog.ploeh.dk/2012/04/17/InjectingHttpControllerContextWithTheASPNETWebAPI.aspx\">implement自定义IHttpControllerActivator 。

This is pretty easy to do using Poor Man's DI: just implement a custom IHttpControllerActivator.

然而,温莎城堡这个突然变得非常困难。 previously,我描述来解决这个问题很令人费解的方式,但它系于PerWebRequest生活方式,事实证明,这种生活方式不自托管方案工作,因为HttpContext.Current是空的。

However, with Castle Windsor this suddenly becomes very difficult. Previously, I've described a very convoluted way to resolve this issue, but it hinges on the PerWebRequest lifestyle, and it turns out that this lifestyle doesn't work in self-hosting scenarios, because HttpContext.Current is empty.

到目前为止,我已经能够通过自定义IHttpControllerActivator传递所需信息作为内联参数Resolve方法,使这项工作:

So far, I've been able to make this work by passing the desired information as an inline argument to the Resolve method from a custom IHttpControllerActivator:

public IHttpController Create(
    HttpControllerContext controllerContext,
    Type controllerType)
{
    var baseUri = new Uri(
        controllerContext
            .Request
            .RequestUri
            .GetLeftPart(UriPartial.Authority));

    return (IHttpController)this.container.Resolve(
        controllerType,
        new { baseUri = baseUri });
}

不过,在默认情况下,如果立即请求类型依赖于这个说法只工作(即如果请求控制器本身依赖于基本URI )。如果基本URI 的依赖在依赖层次埋设较深,但默认情况下不工作,因为内嵌参数不会传播到更深的层次。

However, by default, this only works if the immediately requested type relies on the argument (i.e. if the requested Controller itself depends on the baseUri). If the dependency on baseUri is buried deeper in the dependency hierarchy, it doesn't work by default, because inline arguments aren't propagated to deeper layers.

此行​​为可以用自定义的IDependencyResolver(一温莎城堡的IDependencyResolver,而不是一个ASP.NET Web API的IDependencyResolver)来改变:

This behavior can be changed with a custom IDependencyResolver (a Castle Windsor IDependencyResolver, not an ASP.NET Web API IDependencyResolver):

public class InlineDependenciesPropagatingDependencyResolver :
    DefaultDependencyResolver
{
    protected override CreationContext RebuildContextForParameter(
        CreationContext current, Type parameterType)
    {
        if (parameterType.ContainsGenericParameters)
        {
            return current;
        }

        return new CreationContext(parameterType, current, true);
    }
}

注意真正被传递作为 propagateInlineDependencies 构造函数的参数,而不是,这是默认的实现。

Notice that true is being passed as the propagateInlineDependencies constructor argument instead of false, which is the default implementation.

为了接线与所述InlineDependenciesPropagatingDependencyResolver类一个容器实例,必须以这种方式构成:

In order to wire up a container instance with the InlineDependenciesPropagatingDependencyResolver class, it must be constructed in this way:

this.container = 
    new WindsorContainer(
        new DefaultKernel(
            new InlineDependenciesPropagatingDependencyResolver(),
            new DefaultProxyFactory()),
        new DefaultComponentInstaller());

我不知道这是否是这个问题,如果有一个更好/更简单的方法?

I'm wondering if this is the best solution to this problem, or if there's a better/simpler way?

推荐答案

只是为了完整起见,回答我的克日什托夫·Koźmic(温莎城堡的当前维护者)在Twitter上获得表明,在问题中列出的方法事实上,实现这一特定目标的正确途径。

Just for the sake of completeness, an answer I got from Krzysztof Koźmic (the current maintainer of Castle Windsor) on Twitter indicated that the method outlined in the question is, indeed, the correct way of achieving this particular goal.

(但是,我不能链接到鸣叫,克日什托夫·以来的Twitter账号已被保护(微博都没有公开可见。))

(However, I can't link to that tweet, since Krzysztof's twitter account is protected (tweets are not publicly visible.))

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

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