SingletonPerKey disposing per request in Aspnet
See original GitHub issueHi,
It seems that when registering a service as SingletonPerKey
, and the service implements IDisposable
in aspnet core, it disposes per request, however instance is created only once.
Execution flow is as follows
- First request > initialize service (ctor) > execute > call
.Dispose()
- Second request > execute > call
.Dispose()
This is what i got
scope.Configure(c =>
{
c.Export<DataClientManager>().As<IDataClientManager>().Lifestyle.SingletonPerKey((scope, context) => "global");
});
public interface IDataClientManager
{
IDataClientManager Register(object type);
}
public class DataClientManager : IDataClientManager, IDisposable
{
private readonly HashSet<object> _instances = new HashSet<object>();
public IDataClientManager Register(object type)
{
_instances.Add(type);
return this;
}
public void Dispose()
{
}
}
This causes some services such as GQL to not work properly, as they will Dispose
and next time round it will blow.
I expects that it only Dispose once the server shuts down in the case of singleton
Versions
- Grace 7.1.0
- Grace.AspNetCore.Hosting: 7.0.1
- NetcoreApp 2.2
I have a working sample as well if needed. Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
How to correctly and safely dispose of singletons instances ...
I am looking for guidance on how to correctly and safely dispose of registered singleton instances when my ASP.NET Core 2.0 app is...
Read more >Disposing Injected Services (or: Using Dependency Injection ...
With ASP.NET Core applications, scopes are created with every HTTP request - after the request, services are disposed.
Read more >Four ways to dispose IDisposables in ASP.NET Core
This post presents the options for disposing services in ASP. ... and Singleton services will be disposed when the application is torn down ......
Read more >IServiceProvider Disposal and Scopes with singletons
I.e before disposing the old service provider, build and swap the new service provider into place so that all new requests start creating...
Read more >Ninject ambient scope and deterministic dispose
Something bound as InRequestScope would be reused during the duration of the request and disposed when the request ends.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok I have a fix for it, essentially the wrong disposal context was being used. I’ve released a new pre-release let me know how it works out for you.
As we’ve talked about this over email and it’s resolved I’m going to close this out. If you have any other issues let me know