Skip to content

SingletonPerKey disposing per request in Aspnet #256

Closed

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!

Activity

  1. ipjohnson commented on Apr 8, 2020

    @ipjohnson
    Owner

    I agree it should dispose only at when the host is disposed. Can you email me the sample and I'll take a look at tonight or tomorrow morning.

  2. changed the title [-]SingletonPerKey disposing in Aspnet issue[/-] [+]SingletonPerKey disposing per request in Aspnet[/+] on Apr 8, 2020
  3. ipjohnson commented on Apr 10, 2020

    @ipjohnson
    Owner

    I was able to reproduce this and I'll have some time next week to release a fix. For the next couple days you could try marking the export as externally owned .ExternallyOwned() and it will keep it from being disposed (ever).

  4. added a commit that references this issue on Apr 13, 2020
  5. ipjohnson commented on Apr 13, 2020

    @ipjohnson
    Owner

    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.

  6. stephenlautier commented on Apr 14, 2020

    @stephenlautier
    Author

    Can you confirm if this one only I need to update Grace: 7.1.1-RC793?

  7. stephenlautier commented on Apr 14, 2020

    @stephenlautier
    Author

    After updating to the mentioned version 7.1.1-RC793, SingletonPerKey as my usage isn't working anymore. Basically can't get the data from .GetExtraData from either scope/context inorder to resolve the tenant

    P.S. running the same sample also breaks

  8. ipjohnson commented on Apr 14, 2020

    @ipjohnson
    Owner

    Ok I pushed a newer version and tested it with your repo. Usually the scope & disposal scope should be the same so that you don't get data from a child scope but in your case you actually do need the data from the child scope.

  9. stephenlautier commented on Apr 14, 2020

    @stephenlautier
    Author

    Thanks I retested it again, however its partially solved. In my example the IDataClientManager is not being disposed but the service implementing IHeroDataClient e.g. MockHotsHeroDataClient its still being disposed as it was (and is also SingletonPerKey).

    Further more I retested the actual scenario (e.g. GQL) and it still happens that the Schema is being disposed

  10. ipjohnson commented on Apr 17, 2020

    @ipjohnson
    Owner

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions