-
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ASP.Net Core 3.1 - DbContext (Transient Disposable) store in Scope Container #261
Comments
Likely yes. and the creation of the scope for each query / request seems like a natural choice for Unit-of-work pattern. Otherwise, transient disposables are accumulating at the top level (Singleton) because we need to track them somewhere for disposal, and we have no Scope to put them in. |
Hello, Last question: is there a way/a method to release an instance in the scope? |
The method is Disposing the scope. |
Hello, On StackOverFlow, I asked how to have one scope per http request. So I don't open any Scope. Now if I want to open a scope per HTTP Request, I don't know where I can do it. I am a little bit wonfused. But the memory of my application is growing up and between two snapshots, the Scope size is growing up. Thanks for your help. |
I have released the new DryIoc.Microsoft.DependencyInjection v4 Please check it out. |
Hi, Thanks for your time. |
Hi,
I am using DryIoc (last release version) for Dependency Injection.
In my application (Asp.net Core 3.1), I am using Entity Framework.
My AppDbContext hinerits DbContext and implements IDisposable
I also use UnitOfWork pattern and the class is disposable.
These two objects are declared as Transient.
I follow the documentation that explains the context with Transient Disposable objects:
https://github.com/dadhi/DryIoc/blob/master/docs/DryIoc.Docs/ReuseAndScopes.md
For my AppDbContext, I resolve this service manually. Same thing for my UnitOfWork. At the end I call Dispose method.
But these two instances are not destroyed and are stored in the Singleton Scope of the DryIoc Container.
I did some tests and use JetBrain dotMemory.
My test is to call around 300 times a method
At the end, I have around 300 times my AppDbContext and my UnitOfWork in the scope of the container:
I try a lot of combinaison of creation of container but each time, it is the same thing:
var container = new Container(rules => rules.With(propertiesAndFields: request => request.ServiceType.Name.EndsWith("Controller") ? PropertiesAndFields.Auto(request) : null) // .WithoutThrowOnRegisteringDisposableTransient() // .WithTrackingDisposableTransients() .WithoutThrowIfDependencyHasShorterReuseLifespan()) .WithDependencyInjectionAdapter(services);
Result: memory is growing up fast because of these two kind of objects stored in the scope.If I comment .WithoutThrowOnRegisteringDisposableTransient(), my code is still working (I thought an exception would be thrown)
I tried also to declare these services as Scoped (for each http request) but it does not work because I don't create scope for each query. (Exception thrown and a scope is automatically opened per each web request by Asp .Net Core framework).
Maybe I need to dispose scope at the end of each request?
Thanks for your help.
The text was updated successfully, but these errors were encountered: