Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter not getting instance per http request #525

Closed
richardburns opened this issue May 14, 2014 · 3 comments
Closed

Filter not getting instance per http request #525

richardburns opened this issue May 14, 2014 · 3 comments

Comments

@richardburns
Copy link

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

@tillig
Copy link
Member

tillig commented May 14, 2014

Duplicate of #452.

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.

@richardburns
Copy link
Author

I understand, how would you recommend working around this issue, is the only option to use the GetDependencyScope() from within the filter?

@tillig
Copy link
Member

tillig commented May 15, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants