diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 79aa7774dd..90139ad93e 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -71,6 +71,7 @@ namespace Microsoft.AspNet.Hosting.Internal async features => { var httpContext = contextFactory.CreateHttpContext(features); + httpContext.ApplicationServices = _applicationServices; var requestIdentifier = GetRequestIdentifier(httpContext); using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier)) diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs index 87950f61dc..700fbf6d7e 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs @@ -3,7 +3,6 @@ using Microsoft.AspNet.Builder; using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.OptionsModel; using System; namespace Microsoft.AspNet.Hosting.Fakes diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index c4b486ca45..564b29ab91 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -43,7 +43,7 @@ namespace Microsoft.AspNet.TestHost [Fact] public async Task RequestServicesAutoCreated() { - TestServer server = TestServer.Create(app => + var server = TestServer.Create(app => { app.Run(context => { @@ -55,6 +55,33 @@ namespace Microsoft.AspNet.TestHost Assert.Equal("RequestServices:True", result); } + public class CustomContainerStartup + { + public IServiceProvider Services; + public IServiceProvider ConfigureServices(IServiceCollection services) + { + Services = services.BuildServiceProvider(); + return Services; + } + + public void Configure(IApplicationBuilder app) + { + app.Run(async context => + { + await context.Response.WriteAsync("ApplicationServicesEqual:" + (context.ApplicationServices == Services)); + }); + } + + } + + [Fact] + public async Task CustomServiceProviderReplacesApplicationServices() + { + var server = new TestServer(TestServer.CreateBuilder().UseStartup()); + string result = await server.CreateClient().GetStringAsync("/path"); + Assert.Equal("ApplicationServicesEqual:True", result); + } + public class TestService { } public class TestRequestServiceMiddleware @@ -104,6 +131,29 @@ namespace Microsoft.AspNet.TestHost Assert.Equal("Found:True", result); } + public class EnsureApplicationServicesFilter : IStartupFilter + { + public Action Configure(IApplicationBuilder app, Action next) + { + return builder => + { + app.Run(context => { + Assert.NotNull(context.ApplicationServices); + return context.Response.WriteAsync("Done"); + }); + }; + } + } + + [Fact] + public async Task ApplicationServicesShouldSetBeforeStatupFilters() + { + var server = TestServer.Create(app => { }, + services => services.AddTransient()); + string result = await server.CreateClient().GetStringAsync("/path"); + Assert.Equal("Done", result); + } + [Fact] public async Task CanAccessLogger()