| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- commit 5748898fc42b335de05ef2c1b1586babb8039d50
- Author: David Fowler <[email protected]>
- Date: Wed Mar 21 08:58:03 2018 -0700
- Make the RequestServicesContainerMiddleware thinner (#1360) (#1362)
-
- - Today the request services middleware is responsible for making sure there are request scoped services.
- This PR tries introduces some breaking changes that are hopefully acceptable in order to gain some performance.
- - Here are the assumptions this PR makes:
- - Since this middleware is first in the pipeline, the only thing that can
- set a default service provider would be the server itself. Since we have no servers that do that
- I removed that code that tries to noop if there's an existing service provider.
- - This PR no longer restores the previous service provider feature since it gets replaced every request
- anyways. Kestrel also clears out the feature on each request so it shouldn't be a problem (in theory).
- Once again, since this middleware is first, it is the last thing that runs before the server re-gains
- control on the way out so there's no need to restore anything.
- - We use the RegisterForDispose method to dispose of the IServiceProvider instead of doing it inline.
- diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesContainerMiddleware.cs b/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesContainerMiddleware.cs
- index 8f30c3195c4..22b034ee34c 100644
- --- a/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesContainerMiddleware.cs
- +++ b/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesContainerMiddleware.cs
- @@ -30,34 +30,17 @@ namespace Microsoft.AspNetCore.Hosting.Internal
- _scopeFactory = scopeFactory;
- }
-
- - public async Task Invoke(HttpContext httpContext)
- + public Task Invoke(HttpContext httpContext)
- {
- Debug.Assert(httpContext != null);
-
- // local cache for virtual disptach result
- var features = httpContext.Features;
- - var existingFeature = features.Get<IServiceProvidersFeature>();
-
- - // All done if RequestServices is set
- - if (existingFeature?.RequestServices != null)
- - {
- - await _next.Invoke(httpContext);
- - return;
- - }
- + var servicesFeature = new RequestServicesFeature(httpContext, _scopeFactory);
-
- - var replacementFeature = new RequestServicesFeature(_scopeFactory);
- -
- - try
- - {
- - features.Set<IServiceProvidersFeature>(replacementFeature);
- - await _next.Invoke(httpContext);
- - }
- - finally
- - {
- - replacementFeature.Dispose();
- - // Restore previous feature state
- - features.Set(existingFeature);
- - }
- + features.Set<IServiceProvidersFeature>(servicesFeature);
- + return _next.Invoke(httpContext);
- }
- }
- }
- \ No newline at end of file
- diff --git a/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesFeature.cs b/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesFeature.cs
- index 8d85cec63ec..a57df9bcbc4 100644
- --- a/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesFeature.cs
- +++ b/src/Microsoft.AspNetCore.Hosting/Internal/RequestServicesFeature.cs
- @@ -3,6 +3,7 @@
-
- using System;
- using System.Diagnostics;
- +using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.Extensions.DependencyInjection;
-
- @@ -14,10 +15,12 @@ namespace Microsoft.AspNetCore.Hosting.Internal
- private IServiceProvider _requestServices;
- private IServiceScope _scope;
- private bool _requestServicesSet;
- + private HttpContext _context;
-
- - public RequestServicesFeature(IServiceScopeFactory scopeFactory)
- + public RequestServicesFeature(HttpContext context, IServiceScopeFactory scopeFactory)
- {
- Debug.Assert(scopeFactory != null);
- + _context = context;
- _scopeFactory = scopeFactory;
- }
-
- @@ -27,6 +30,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
- {
- if (!_requestServicesSet)
- {
- + _context.Response.RegisterForDispose(this);
- _scope = _scopeFactory.CreateScope();
- _requestServices = _scope.ServiceProvider;
- _requestServicesSet = true;
|