开发者

How to register Autofac component per http request in web.config?

开发者 https://www.devze.com 2023-04-12 13:37 出处:网络
In MVC3 application I h开发者_开发百科ave my custom implementation of IPrincipal. I want to register it in web.config and then inject it to constructors by Autofac.

In MVC3 application I h开发者_开发百科ave my custom implementation of IPrincipal.

I want to register it in web.config and then inject it to constructors by Autofac.

Autofac XML-configuration has attribute "instance-scope" but it does not support "per-http-request".

Any ideas how can I register component per http scope in web.config?

I already tried different combinations of per-dependency, single-instance or per-lifetime-scope with ownership external but results are very buggy.


As Nicholas Blumhardt from Autofac team told me per-lifetime-scope should be used.

When running under ASP.NET it is equivalent to per-HTTP-request in practical terms.


As far as I can see from the source, registering per http request in XML configuration is not supported. As you mention, only the three instance-scope values (per-dependency, single-instance and per-lifetime-scope) are allowed.

Also, looking at how per-http-request scoping works, it uses the InstancePerMatchingLifetimeScope registration call with a httpRequest key. Again, there is no XML config way of configuring a "matching-lifetime-scope".

The way to go is configuration through code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号