Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin Context to use a single instance per request

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.

like image 472
DarthVader Avatar asked Apr 20 '26 07:04

DarthVader


1 Answers

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.

like image 166
Tobias Avatar answered Apr 21 '26 20:04

Tobias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!