Explicitly set ApplicationServices for HttpContext
This commit is contained in:
parent
7f5045d62f
commit
55b28abeab
|
|
@ -71,6 +71,7 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
async features =>
|
async features =>
|
||||||
{
|
{
|
||||||
var httpContext = contextFactory.CreateHttpContext(features);
|
var httpContext = contextFactory.CreateHttpContext(features);
|
||||||
|
httpContext.ApplicationServices = _applicationServices;
|
||||||
var requestIdentifier = GetRequestIdentifier(httpContext);
|
var requestIdentifier = GetRequestIdentifier(httpContext);
|
||||||
|
|
||||||
using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier))
|
using (logger.BeginScope("Request Id: {RequestId}", requestIdentifier))
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
using Microsoft.AspNet.Builder;
|
using Microsoft.AspNet.Builder;
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Hosting.Fakes
|
namespace Microsoft.AspNet.Hosting.Fakes
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ namespace Microsoft.AspNet.TestHost
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task RequestServicesAutoCreated()
|
public async Task RequestServicesAutoCreated()
|
||||||
{
|
{
|
||||||
TestServer server = TestServer.Create(app =>
|
var server = TestServer.Create(app =>
|
||||||
{
|
{
|
||||||
app.Run(context =>
|
app.Run(context =>
|
||||||
{
|
{
|
||||||
|
|
@ -55,6 +55,33 @@ namespace Microsoft.AspNet.TestHost
|
||||||
Assert.Equal("RequestServices:True", result);
|
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 TestService { }
|
||||||
|
|
||||||
public class TestRequestServiceMiddleware
|
public class TestRequestServiceMiddleware
|
||||||
|
|
@ -104,6 +131,29 @@ namespace Microsoft.AspNet.TestHost
|
||||||
Assert.Equal("Found:True", result);
|
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]
|
[Fact]
|
||||||
public async Task CanAccessLogger()
|
public async Task CanAccessLogger()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue