Allocate the IServiceScopeFactory once and reuse it across requests

- Since we resolve the IServiceProvider from the application container, it's should be safe to reuse it across requests without the extra lookup everytime.
This commit is contained in:
David Fowler 2016-04-01 09:50:14 -07:00
parent 8c69989a5f
commit b6a3fee088
2 changed files with 13 additions and 12 deletions

View File

@ -5,26 +5,27 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Hosting.Internal namespace Microsoft.AspNetCore.Hosting.Internal
{ {
public class RequestServicesContainerMiddleware public class RequestServicesContainerMiddleware
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly IServiceProvider _services; private IServiceScopeFactory _scopeFactory;
public RequestServicesContainerMiddleware(RequestDelegate next, IServiceProvider services) public RequestServicesContainerMiddleware(RequestDelegate next, IServiceScopeFactory scopeFactory)
{ {
if (next == null) if (next == null)
{ {
throw new ArgumentNullException(nameof(next)); throw new ArgumentNullException(nameof(next));
} }
if (services == null) if (scopeFactory == null)
{ {
throw new ArgumentNullException(nameof(services)); throw new ArgumentNullException(nameof(scopeFactory));
} }
_services = services; _scopeFactory = scopeFactory;
_next = next; _next = next;
} }
@ -44,7 +45,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
return; return;
} }
using (var feature = new RequestServicesFeature(_services)) using (var feature = new RequestServicesFeature(_scopeFactory))
{ {
try try
{ {

View File

@ -9,19 +9,19 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{ {
public class RequestServicesFeature : IServiceProvidersFeature, IDisposable public class RequestServicesFeature : IServiceProvidersFeature, IDisposable
{ {
private IServiceProvider _appServices; private IServiceScopeFactory _scopeFactory;
private IServiceProvider _requestServices; private IServiceProvider _requestServices;
private IServiceScope _scope; private IServiceScope _scope;
private bool _requestServicesSet; private bool _requestServicesSet;
public RequestServicesFeature(IServiceProvider applicationServices) public RequestServicesFeature(IServiceScopeFactory scopeFactory)
{ {
if (applicationServices == null) if (scopeFactory == null)
{ {
throw new ArgumentNullException(nameof(applicationServices)); throw new ArgumentNullException(nameof(scopeFactory));
} }
_appServices = applicationServices; _scopeFactory = scopeFactory;
} }
public IServiceProvider RequestServices public IServiceProvider RequestServices
@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{ {
if (!_requestServicesSet) if (!_requestServicesSet)
{ {
_scope = _appServices.GetRequiredService<IServiceScopeFactory>().CreateScope(); _scope = _scopeFactory.CreateScope();
_requestServices = _scope.ServiceProvider; _requestServices = _scope.ServiceProvider;
_requestServicesSet = true; _requestServicesSet = true;
} }