Skip to content

SingletonPerKey disposing per request in Aspnet #256

Description

@stephenlautier

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions