相关疑难解决方法(0)

使用Castle Windsor解析HttpControllerContext

ASP.NET Web API中,HttpControllerContext实例提供了有关当前环境的大量信息,包括当前请求的URI.

如果服务依赖于此类信息(例如请求URI),则应该可以将该信息注入服务中.

使用Poor Man的DI很容易做到:只需实现一个自定义的IHttpControllerActivator.

然而,随着温莎城堡,这突然变得非常困难.以前,我已经描述了解决这个问题的一种非常复杂的方法,但它取决于PerWebRequest的生活方式,并且事实证明这种生活方式在自托管方案中不起作用,因为HttpContext.Current是空的.

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

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 });
}
Run Code Online (Sandbox Code Playgroud)

但是,默认情况下,仅当立即请求的类型依赖于参数时才会起作用(即,如果请求的Controller本身依赖于该参数baseUri).如果依赖关系baseUri更深层地隐藏在依赖关系层次结构中,则默认情况下它不起作用,因为内联参数不会传播到更深层.

可以使用自定义IDependencyResolver(Castle Windsor IDependencyResolver,而不是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); …
Run Code Online (Sandbox Code Playgroud)

dependency-injection castle-windsor self-hosting asp.net-web-api

27
推荐指数
1
解决办法
2155
查看次数