Owin Context to use a single instance per request

核能气质少年 提交于 2019-12-10 21:53:07

问题


app.CreatePerOwinContext(DataContext.Create);

I have the above line to create a data context this code came with project when i kick a new MVC 5 project. At the same time, i am using autofac to inject single instance per request in my dependency registrar.

builder.RegisterType<DataContext>().As<IDbContext>().InstancePerRequest();

What is the best way to use owin and DI container?

I have a DataContext class which has a static Method called Create as above and this break when I try to inject a logger in a database class for example.

I am also interested in a sample project if any , demonstrating DI and owin in the same project.


回答1:


It is not necessary to use both OWIN and a DI container. I can understand your confusion, since the documentation on CreatePerOwinContext is very sparse, but the truth is that this feature is only implemented as a "poor man's DI", and should probably be replaced by a normal DI framework. Microsoft's Hao Kung, who is on the ASP.NET team says you are free to replace it with a proper DI framework.

If you're not fetching the instance created by the OWIN middleware, and instead injecting your context into your controllers, then you can safely remove it. Just remember to update the scaffolded controllers accordingly.



来源:https://stackoverflow.com/questions/25388656/owin-context-to-use-a-single-instance-per-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!