You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using with WebApi the filter is not getting the same instance per http request injected to the property. The instance on the filter is not the same instance as on the controller, you can get the same controller instance on the filter using:
var service = actionContext.Request.GetDependencyScope().GetService(typeof(IOurService);
Web API caches instances of filters in the action descriptor after the initial creation of the filter. The mechanism the strathweb article uses is sort of "service location" from inside the filter, which is totally possible with Autofac and will get you the request lifetime scope.
In order to actually get request lifetime instances of filters, we'd have to do some pretty crazy dynamic proxy wrapping of each filter such that the instance getting cached is the proxy and then the actual thing executing is a resolved instance. I'm not sure we want to take it that far. I have a feeling that'd be pretty hard to maintain and would interfere with other systems which might want to query the list of applied filters, etc.
Unfortunately, yes. The only way to get the request scope inside the cached filter instance is to do that GetDependencyScope service location trick as shown in the blog article.
When using with WebApi the filter is not getting the same instance per http request injected to the property. The instance on the filter is not the same instance as on the controller, you can get the same controller instance on the filter using:
var service = actionContext.Request.GetDependencyScope().GetService(typeof(IOurService);
But it should default to this on the filter.
More details:
http://www.strathweb.com/2012/11/asp-net-web-api-and-dependencies-in-request-scope/
And:
http://stackoverflow.com/questions/23659108/webapi-autofac-system-web-http-filters-actionfilterattribute-instance-per-requ
The text was updated successfully, but these errors were encountered: