Explicitly set ApplicationServices for HttpContext

This commit is contained in:
Hao Kung 2015-08-25 12:23:56 -07:00
parent 7f5045d62f
commit 55b28abeab
3 changed files with 52 additions and 2 deletions

View File

@ -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))

View File

@ -3,7 +3,6 @@
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
using System;
namespace Microsoft.AspNet.Hosting.Fakes

View File

@ -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<CustomContainerStartup>());
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<IApplicationBuilder> Configure(IApplicationBuilder app, Action<IApplicationBuilder> 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<IStartupFilter, EnsureApplicationServicesFilter>());
string result = await server.CreateClient().GetStringAsync("/path");
Assert.Equal("Done", result);
}
[Fact]
public async Task CanAccessLogger()