-
Notifications
You must be signed in to change notification settings - Fork 101
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
Get or Create Service and IDisposable #13
Comments
Don't use that method to create disposable instances. Seriously though, we should document it, it's a flaw in the design. |
hi @davidfowl . another question related to this. There are a few cases where the |
@ExceptionCaught A common use-case for this, is when you have "incompatible" lifetimes. where you need to consume a scoped service inside of a singleton service. For example. using a Database Context (scoped) in Hosted Services (singleton). |
hi @BorisWilhelms . in this case, the |
@ExceptionCaught In your singleton, you will have to create your own scope, like
Try to keep the scope's lifecycle as short as possible. |
There is a note at the bottom of this section: https://github.com/davidfowl/DotNetCodingPatterns/blob/main/1.md#creating-instances-of-types-from-an-iserviceprovider
It's interesting to note that
ActivatorUtilities
also has aGetServiceOrCreateInstance
method. However its not possible to tell when calling this API, if the instance returned was resolved from a registration, or was not registered and so a new instance was created. Given the note above, if the service wasIDisposable
- how can the consumer know if they are, or are not responsible for disposing the instance when obtaining services like this? It feels like theGetServiceOrCreateInstance
method should supply anout
parameter indicating whether the service was returned and is being tracked, or whether an instance was created and therefore is not being tracked ?The text was updated successfully, but these errors were encountered: