Reputation: 55022
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.
Upvotes: 4
Views: 4285
Reputation: 2840
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.
Upvotes: 2