question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SingletonPerKey disposing per request in Aspnet

See original GitHub issue

Hi,

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:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ipjohnsoncommented, Apr 13, 2020

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.

1reaction
ipjohnsoncommented, Apr 17, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found