Skip to content
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

[BUG] Splat.Microsoft.Extensions.DependencyInjection never disposes the service locator #1168

Open
calebkiage opened this issue Jul 30, 2024 · 1 comment
Labels

Comments

@calebkiage
Copy link

Describe the bug
The .NET ServiceProvider can dispose services with managed resources according to the Disposal of Services docs, but this only works if the service provider instance is also disposed properly. However, the MicrosoftDependencyResolver class:

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes of the instance.
/// </summary>
/// <param name="disposing">Whether or not the instance is disposing.</param>
protected virtual void Dispose(bool disposing)
{
}

never disposes the container. This affects services that need to do work to clean up resources when they're disposed.

Steps To Reproduce
Provide the steps to reproduce the behavior:

  1. Create a ServiceCollection
  2. Register an IDisposable service and add a log to the dispose method
  3. Call ServiceCollection.UseMicrosoftDependencyResolver on your service collection
  4. Use an instance of the service
  5. Exit the application
  6. The message will not be displayed.

Expected behavior
I expect that the service provider should be disposed when the container is disposed

Screenshots
If applicable, add screenshots to help explain your problem.

Environment(please complete the following information):

  • OS: Windows
  • Version: 11
  • Device: PC

Additional context
When I use a custom class that disposes the ServiceProvider in use and also manually call:

if (Locator.Current is IDisposable d)
{
    d.Dispose();
}

The service is disposed correctly.

@Snailya
Copy link

Snailya commented Sep 20, 2024

Meets the same problem.
When calling the Locater.Current.GetService to get the service, it does not call something like ServiceProviderEngineScope.CaptureDisposable to check if the service is IDisposable, so it is not disposed after the scope is disposed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants