的WebAPI,Autofac,System.Web.Http.Filters.ActionFilterAttribute实例每请求 [英] WebApi, Autofac, System.Web.Http.Filters.ActionFilterAttribute Instance Per Request

查看:1086
本文介绍了的WebAPI,Autofac,System.Web.Http.Filters.ActionFilterAttribute实例每请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在使用Autofac在我们的应用程序(MVC 4现在)很长一段时间,我们有几十个基本控制一切从继承的属性,它已因此,当请求开始创建我们的服务都被做工精细和然后可通过所有属性,并在控制器动作。

We have been using Autofac in our application (MVC 4 now) for a long time, we have dozens of attributes on the base controller everything inherits from and it has all been working fine so when the request begins our service is created and then available through all the attributes and on the controller action.

我们正在寻找的WebAPI和已经创建了我们的WebAPI控制器和创建使用ActionFilterAttribute从HTTP命名空间的基础控制器上的属性。然而这里的问题开始的地方注入上的属性的属性,服务不相同的实例作为对ApiController。看着下面这个链接似乎被称为 ASP .NET的Web API,并在请求范围依赖

We are now looking at WebApi and have created our WebApi controller and created an attribute on the base controller using the ActionFilterAttribute from the HTTP namespace. However the problem starts here where the service injected on the property on the attribute is not the same instance as that on the ApiController. Looking at the link below this seems to be known ASP.NET Web API and dependencies in request scope

因为我们不希望我们的控制器来了解依赖注入但是这里的解决方案并不理想,我们只想用我们注入到物业服务,并知道它是每个请求一个实例。

However the solution here is not ideal as we don't want our controllers to know about the dependency injection, we just want to use the service we are injecting to the property and know it is one instance per request.

我们呼吁这样的:

  builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);

  GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

我们的课程目前正与Autofac作为InstancePerLifetimeScope,注册我们想要的是能够每请求MvcControllers和ApiControllers工作有。

Our classes are currently registered with Autofac as InstancePerLifetimeScope, what we want is to be able to have per request working for MvcControllers and ApiControllers.

这可能吗?

编辑:

所以基本上这条线返回请求正确的服务(即同一个实例,这也是对ApiController)

So basically this line returns the right service for the request (i.e. the same instance that is also on the ApiController)

var service = actionContext.Request.GetDependencyScope().GetService(typeof(IOurService);

但在ActionFilterAttribute物业注入的实例是不一样的,如果我改变Autofac注册成为InstancePerApiRequest我收到以下错误:

But the property injection instance on the ActionFilterAttribute is not the same and if I change the Autofac registration to be InstancePerApiRequest I get the following error:

不带标签匹配'AutofacWebRequest'的范围是从其中要求该实例的范围是可见的。这通常表明组件注册为每个HTTP正在请求要求由SingleInstance()组件(或类似的场景。)在网络整合总是要求依赖从DependencyResolver.Current或ILifetimeScopeProvider.RequestLifetime,永远不会从容器本身。

"No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself."

推荐答案

这是一个已知的问题,并在网页API设计问题。当网页API首先创建的过滤器实例,它们被缓存,所以Autofac必须解决使用root寿命范围,而不是请求生命周期范围内的财产注入。有没有机会对每个请求的基础Autofac做任何财产注射 - 过滤器是有效的Web API中的单身并且没有钩来改变

This is a known issue and a design problem in Web API. When filter instances are first created in Web API they are cached, so Autofac has to resolve the property injection using the root lifetime scope, not the request lifetime scope. There is no opportunity on a per-request basis for Autofac to do any property injection - the filters are effectively singletons within Web API and there's no hook to change that.

之后,如果你在你的过滤器需要每个请求的服务,你必须使用 GetDependencyScope()欺骗。

Thereafter, if you need per-request services in your filter, you have to use that GetDependencyScope() trick.

查看Autofac这些问题的更多细节:

See these issues on Autofac for more details:

  • Issue #452: Property Injection in Web API ActionFilterAttribute does not use Http request scope
  • Issue #525: Filter not getting instance per http request

这篇关于的WebAPI,Autofac,System.Web.Http.Filters.ActionFilterAttribute实例每请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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