From 16fee38c957c131bf1fcf86c4135c8d0e3179cc0 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Tue, 14 Oct 2014 21:02:33 -0700 Subject: [PATCH] Handle null httpContext.ApplicationServices --- .../RequestServicesContainer.cs | 18 ++++++++++-------- .../UseRequestServicesFacts.cs | 13 +++++++++---- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.AspNet.RequestContainer/RequestServicesContainer.cs b/src/Microsoft.AspNet.RequestContainer/RequestServicesContainer.cs index c4bbf142c2..921cfcc425 100644 --- a/src/Microsoft.AspNet.RequestContainer/RequestServicesContainer.cs +++ b/src/Microsoft.AspNet.RequestContainer/RequestServicesContainer.cs @@ -55,7 +55,7 @@ namespace Microsoft.AspNet.RequestContainer private IContextAccessor AppContextAccessor { get; set; } // CONSIDER: this could be an extension method on HttpContext instead - public static RequestServicesContainer EnsureRequestServices(HttpContext httpContext) + public static RequestServicesContainer EnsureRequestServices(HttpContext httpContext, IServiceProvider services) { // All done if we already have a request services if (httpContext.RequestServices != null) @@ -63,21 +63,23 @@ namespace Microsoft.AspNet.RequestContainer return null; } - if (httpContext.ApplicationServices == null) + var serviceProvider = httpContext.ApplicationServices ?? services; + + if (serviceProvider == null) { - throw new InvalidOperationException("TODO: httpContext.ApplicationServices is null!"); + throw new InvalidOperationException("TODO: services and httpContext.ApplicationServices are both null!"); } // Matches constructor of RequestContainer - var rootServiceProvider = httpContext.ApplicationServices.GetService(); - var rootHttpContextAccessor = httpContext.ApplicationServices.GetService>(); - var rootServiceScopeFactory = httpContext.ApplicationServices.GetService(); + var rootServiceProvider = serviceProvider.GetService(); + var rootHttpContextAccessor = serviceProvider.GetService>(); + var rootServiceScopeFactory = serviceProvider.GetService(); rootHttpContextAccessor.SetContextSource(ContainerMiddleware.AccessRootHttpContext, ContainerMiddleware.ExchangeRootHttpContext); // Pre Scope setup - var priorApplicationServices = httpContext.ApplicationServices; - var priorRequestServices = httpContext.RequestServices; + var priorApplicationServices = serviceProvider; + var priorRequestServices = serviceProvider; var appServiceProvider = rootServiceProvider; var appServiceScopeFactory = rootServiceScopeFactory; diff --git a/test/Microsoft.AspNet.Hosting.Tests/UseRequestServicesFacts.cs b/test/Microsoft.AspNet.Hosting.Tests/UseRequestServicesFacts.cs index 597c286054..1229df1bb4 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/UseRequestServicesFacts.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/UseRequestServicesFacts.cs @@ -40,8 +40,10 @@ namespace Microsoft.AspNet.Hosting.Tests Assert.True(foundRequestServicesAfter); } - [Fact] - public void EnsureRequestServicesSetsRequestServices() + [Theory] + [InlineData(true)] + [InlineData(false)] + public void EnsureRequestServicesSetsRequestServices(bool initializeApplicationServices) { var baseServiceProvider = new ServiceCollection() .Add(HostingServices.GetDefaultServices()) @@ -56,7 +58,7 @@ namespace Microsoft.AspNet.Hosting.Tests }); builder.Use(next => async c => { - using (var container = RequestServicesContainer.EnsureRequestServices(c)) + using (var container = RequestServicesContainer.EnsureRequestServices(c, baseServiceProvider)) { await next.Invoke(c); } @@ -69,7 +71,10 @@ namespace Microsoft.AspNet.Hosting.Tests }); var context = new DefaultHttpContext(); - context.ApplicationServices = baseServiceProvider; + if (initializeApplicationServices) + { + context.ApplicationServices = baseServiceProvider; + } builder.Build().Invoke(context); Assert.False(foundRequestServicesBefore); Assert.True(foundRequestServicesAfter);