I am working on an ASP.NET MVC web application that uses ServiceStack and EF. In my AppHost I configure Funq to default to Request reuse scope:
container.DefaultReuse = ReuseScope.Request;
Then I register my EF context.
When running unit tests (i.e. when no HTTP request exists) I get the "DbContext disposed" exception. I assume it's because my context is immediately disposed by Funq in the absence of the HTTP request. Right?
I have previous experience with StructureMap that supports a hybrid reuse (i.e. either per-request if HTTP context exists or per-thread), so how would I configure Func to do the same for me?
My bad! Turns out this is not Funq's issue but that of the EF database migrations:
Apparently migrations logic cleans up after itself by disposing of the context. My problem was that I used IDbContextFactory to provide the context to migrations, and inside its Create method I was returning the instance of the context resolved by Funq's container. Which is how the instance in the container ended up disposed after migrations was done with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With