runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Add IServiceScopeFactory.CreateAsyncScope

Open davidfowl opened this issue 5 years ago • 8 comments

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

davidfowl avatar May 01 '21 08:05 davidfowl

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:

api-suggestion, area-Extensions-DependencyInjection, untriaged

Milestone: -

ghost avatar May 01 '21 08:05 ghost

@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?

eerhardt avatar May 07 '21 17:05 eerhardt

Yes please

davidfowl avatar May 07 '21 17:05 davidfowl

Works for me

terrajobst avatar May 07 '21 19:05 terrajobst

Just an overload that takes IServiceScopeFactory instead of IServiceScope? :shipit:

bartonjs avatar May 07 '21 19:05 bartonjs

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.

eerhardt avatar May 07 '21 20:05 eerhardt

So this API is approved :shipit: ?

davidfowl avatar May 13 '21 13:05 davidfowl

I'm working on this. I will open the PR.

ilkayilknur avatar May 13 '21 17:05 ilkayilknur