Handle null httpContext.ApplicationServices

This commit is contained in:
Hao Kung 2014-10-14 21:02:33 -07:00
parent 6466d1061e
commit 16fee38c95
2 changed files with 19 additions and 12 deletions

View File

@ -55,7 +55,7 @@ namespace Microsoft.AspNet.RequestContainer
private IContextAccessor<HttpContext> AppContextAccessor { get; set; } private IContextAccessor<HttpContext> AppContextAccessor { get; set; }
// CONSIDER: this could be an extension method on HttpContext instead // 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 // All done if we already have a request services
if (httpContext.RequestServices != null) if (httpContext.RequestServices != null)
@ -63,21 +63,23 @@ namespace Microsoft.AspNet.RequestContainer
return null; 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 // Matches constructor of RequestContainer
var rootServiceProvider = httpContext.ApplicationServices.GetService<IServiceProvider>(); var rootServiceProvider = serviceProvider.GetService<IServiceProvider>();
var rootHttpContextAccessor = httpContext.ApplicationServices.GetService<IContextAccessor<HttpContext>>(); var rootHttpContextAccessor = serviceProvider.GetService<IContextAccessor<HttpContext>>();
var rootServiceScopeFactory = httpContext.ApplicationServices.GetService<IServiceScopeFactory>(); var rootServiceScopeFactory = serviceProvider.GetService<IServiceScopeFactory>();
rootHttpContextAccessor.SetContextSource(ContainerMiddleware.AccessRootHttpContext, ContainerMiddleware.ExchangeRootHttpContext); rootHttpContextAccessor.SetContextSource(ContainerMiddleware.AccessRootHttpContext, ContainerMiddleware.ExchangeRootHttpContext);
// Pre Scope setup // Pre Scope setup
var priorApplicationServices = httpContext.ApplicationServices; var priorApplicationServices = serviceProvider;
var priorRequestServices = httpContext.RequestServices; var priorRequestServices = serviceProvider;
var appServiceProvider = rootServiceProvider; var appServiceProvider = rootServiceProvider;
var appServiceScopeFactory = rootServiceScopeFactory; var appServiceScopeFactory = rootServiceScopeFactory;

View File

@ -40,8 +40,10 @@ namespace Microsoft.AspNet.Hosting.Tests
Assert.True(foundRequestServicesAfter); Assert.True(foundRequestServicesAfter);
} }
[Fact] [Theory]
public void EnsureRequestServicesSetsRequestServices() [InlineData(true)]
[InlineData(false)]
public void EnsureRequestServicesSetsRequestServices(bool initializeApplicationServices)
{ {
var baseServiceProvider = new ServiceCollection() var baseServiceProvider = new ServiceCollection()
.Add(HostingServices.GetDefaultServices()) .Add(HostingServices.GetDefaultServices())
@ -56,7 +58,7 @@ namespace Microsoft.AspNet.Hosting.Tests
}); });
builder.Use(next => async c => builder.Use(next => async c =>
{ {
using (var container = RequestServicesContainer.EnsureRequestServices(c)) using (var container = RequestServicesContainer.EnsureRequestServices(c, baseServiceProvider))
{ {
await next.Invoke(c); await next.Invoke(c);
} }
@ -69,7 +71,10 @@ namespace Microsoft.AspNet.Hosting.Tests
}); });
var context = new DefaultHttpContext(); var context = new DefaultHttpContext();
context.ApplicationServices = baseServiceProvider; if (initializeApplicationServices)
{
context.ApplicationServices = baseServiceProvider;
}
builder.Build().Invoke(context); builder.Build().Invoke(context);
Assert.False(foundRequestServicesBefore); Assert.False(foundRequestServicesBefore);
Assert.True(foundRequestServicesAfter); Assert.True(foundRequestServicesAfter);