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

Get or Create Service and IDisposable #13

Open
dazinator opened this issue Mar 1, 2021 · 5 comments
Open

Get or Create Service and IDisposable #13

dazinator opened this issue Mar 1, 2021 · 5 comments

Comments

@dazinator
Copy link

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

NOTE: Disposable instances created with this API will not be disposed by the DI container.

It's interesting to note that ActivatorUtilities also has a GetServiceOrCreateInstance 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 was IDisposable - how can the consumer know if they are, or are not responsible for disposing the instance when obtaining services like this? It feels like the GetServiceOrCreateInstance method should supply an out parameter indicating whether the service was returned and is being tracked, or whether an instance was created and therefore is not being tracked ?

@davidfowl
Copy link
Owner

Don't use that method to create disposable instances. Seriously though, we should document it, it's a flaw in the design.

@ExceptionCaught
Copy link

hi @davidfowl . another question related to this. There are a few cases where the IServiceProvider got injected into the constructor. Isn't this a service locator pattern? A few articles on the inernet see this as a anti-pattern and will not suggest to do so. What is the main factor when considering such pattern?

@BorisWilhelms
Copy link

@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).

@ExceptionCaught
Copy link

hi @BorisWilhelms . in this case, the Hosted Services is singleton and initiated during the application starts, does it mean we will do
IServiceProvider.GetService<DBContext>() in one of the methods. but not in the constructor?

@thoemmi
Copy link

thoemmi commented May 3, 2021

@ExceptionCaught In your singleton, you will have to create your own scope, like

using (var scope = serviceProvider.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetService<DBContext>();
    // ...
}

Try to keep the scope's lifecycle as short as possible.

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

No branches or pull requests

5 participants