dazinator / Dazinator.Extensions

Useful additions to Microsoft.Extensions.DependencyInjection such as Named Services.
Other
38 stars 1 forks source link

Child service provider should create parent "scope" to resolve from parent #23

Closed dazinator closed 3 years ago

dazinator commented 3 years ago

The Child Service Provider in some situations, uses the parent service provider to resolve certain services.

The problem is, if the parent service provider is disposed, the child service provider will start erroring in these cases.

The solution to this, is to make the Child Service Provider obtain a new Scope from the parent service provider, and to use this parent service provider's Scope to do the resolution in. When you Dispose of the parent service provider, this will break it's scopes (potentially) so this in and of itself won't fix the problem - however, with the advent of #19 you can use a scope tracking service provider as the parent service provider, which will wait for all of its scopes to dispose before it disposes. This means:

  1. If you attempt to dispose async the parent scope tracking service provider #19 and you have a child service provider derived from it, that child service provider will now be consuming a scope - so the parent scope tracking service provider #19 will not dispose until that scope has been released. This basically means it won't dispose until the child service provider has first been disposed which will fix various issues that may otherwise occur.