Add IServiceScopeFactory.CreateAsyncScope
Background and Motivation
As a follow up to https://github.com/dotnet/runtime/issues/43970, we missed one other extension method hanging off IServiceScopeFactory that is commonly used to create scopes as well.
Proposed API
namespace Microsoft.Extensions.DependencyInjection
{
+ public partial class ServiceProviderServiceExtensions
+ {
+ public static AsyncServiceScope CreateAsyncScope(this IServiceScopeFactory seviceScopeFactory)
+ {
+ return new AsyncServiceScope(seviceScopeFactory.CreateScope());
+ }
+ }
}
Usage Examples
async Task ExecuteAsync(IServiceScopeFactory scopeFactory)
{
await using AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
scope.ServiceProvider.GetService(...);
}
Alternative Designs
Do nothing, we already added the as public API so you can always do this:
async Task ExecuteAsync(IServiceScopeFactory scopeFactory)
{
await using AsyncServiceScope scope = new(scopeFactory.CreateScope());
scope.ServiceProvider.GetService(...);
}
Risks
None
Tagging subscribers to this area: @eerhardt, @maryamariyan See info in area-owners.md if you want to be subscribed.
Issue Details
Background and Motivation
As a follow up to https://github.com/dotnet/runtime/issues/43970, we missed one other extension method hanging off IServiceScopeFactory that is commonly used to create scopes as well.
Proposed API
namespace Microsoft.Extensions.DependencyInjection
{
+ public partial class ServiceProviderServiceExtensions
+ {
+ public static AsyncServiceScope CreateAsyncScope(this IServiceScopeFactory seviceScopeFactory)
+ {
+ return new AsyncServiceScope(seviceScopeFactory.CreateScope());
+ }
+ }
}
Usage Examples
async Task ExecuteAsync(IServiceScopeFactory scopeFactory)
{
await using AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
scope.ServiceProvider.GetService(...);
}
Alternative Designs
Do nothing, we already added the as public API so you can always do this:
async Task ExecuteAsync(IServiceScopeFactory scopeFactory)
{
await using AsyncServiceScope scope = new(scopeFactory.CreateScope());
scope.ServiceProvider.GetService(...);
}
Risks
None
| Author: | davidfowl |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
@davidfowl @bartonjs @terrajobst - is this something that could be handled over email? It is pretty straight forward and I don't think needs a dedicated discussion. Thoughts?
Yes please
Works for me
Just an overload that takes IServiceScopeFactory instead of IServiceScope? :shipit:
Just an overload that takes IServiceScopeFactory instead of IServiceScope?
Correct. We added the AsyncServiceScope CreateAsyncScope(this IServiceProvider) method in #51840. This just adds an overload to that that takes IServiceScopeFactory instead.
So this API is approved :shipit: ?
I'm working on this. I will open the PR.