Reacting to ApplicationServices removal from HttpContext
This commit is contained in:
parent
04c30c8bb7
commit
73fd257844
|
|
@ -96,8 +96,6 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
Server.Start(
|
Server.Start(
|
||||||
async httpContext =>
|
async httpContext =>
|
||||||
{
|
{
|
||||||
httpContext.ApplicationServices = _applicationServices;
|
|
||||||
|
|
||||||
if (diagnosticSource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest"))
|
if (diagnosticSource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest"))
|
||||||
{
|
{
|
||||||
diagnosticSource.Write("Microsoft.AspNet.Hosting.BeginRequest", new { httpContext = httpContext });
|
diagnosticSource.Write("Microsoft.AspNet.Hosting.BeginRequest", new { httpContext = httpContext });
|
||||||
|
|
|
||||||
|
|
@ -21,23 +21,7 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
throw new ArgumentNullException(nameof(applicationServices));
|
throw new ArgumentNullException(nameof(applicationServices));
|
||||||
}
|
}
|
||||||
|
|
||||||
ApplicationServices = applicationServices;
|
_appServices = applicationServices;
|
||||||
}
|
|
||||||
|
|
||||||
public IServiceProvider ApplicationServices
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _appServices;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (value == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(value));
|
|
||||||
}
|
|
||||||
_appServices = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IServiceProvider RequestServices
|
public IServiceProvider RequestServices
|
||||||
|
|
@ -46,7 +30,7 @@ namespace Microsoft.AspNet.Hosting.Internal
|
||||||
{
|
{
|
||||||
if (!_requestServicesSet)
|
if (!_requestServicesSet)
|
||||||
{
|
{
|
||||||
_scope = ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
_scope = _appServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
|
||||||
_requestServices = _scope.ServiceProvider;
|
_requestServices = _scope.ServiceProvider;
|
||||||
_requestServicesSet = true;
|
_requestServicesSet = true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ using Microsoft.AspNet.Http.Features;
|
||||||
using Microsoft.AspNet.Http.Features.Internal;
|
using Microsoft.AspNet.Http.Features.Internal;
|
||||||
using Microsoft.AspNet.Http.Internal;
|
using Microsoft.AspNet.Http.Internal;
|
||||||
using Microsoft.AspNet.Testing.xunit;
|
using Microsoft.AspNet.Testing.xunit;
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.DiagnosticAdapter;
|
using Microsoft.Extensions.DiagnosticAdapter;
|
||||||
|
|
@ -59,16 +58,17 @@ namespace Microsoft.AspNet.TestHost
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
|
var applicationServices = app.ApplicationServices;
|
||||||
app.Run(async context =>
|
app.Run(async context =>
|
||||||
{
|
{
|
||||||
await context.Response.WriteAsync("ApplicationServicesEqual:" + (context.ApplicationServices == Services));
|
await context.Response.WriteAsync("ApplicationServicesEqual:" + (applicationServices == Services));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CustomServiceProviderReplacesApplicationServices()
|
public async Task CustomServiceProviderSetsApplicationServices()
|
||||||
{
|
{
|
||||||
var server = new TestServer(TestServer.CreateBuilder().UseStartup<CustomContainerStartup>());
|
var server = new TestServer(TestServer.CreateBuilder().UseStartup<CustomContainerStartup>());
|
||||||
string result = await server.CreateClient().GetStringAsync("/path");
|
string result = await server.CreateClient().GetStringAsync("/path");
|
||||||
|
|
@ -124,22 +124,6 @@ namespace Microsoft.AspNet.TestHost
|
||||||
Assert.Equal("Found:True", result);
|
Assert.Equal("Found:True", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task SettingApplicationServicesOnFeatureToNullThrows()
|
|
||||||
{
|
|
||||||
var server = TestServer.Create(app =>
|
|
||||||
{
|
|
||||||
app.Run(context =>
|
|
||||||
{
|
|
||||||
var feature = context.Features.Get<IServiceProvidersFeature>();
|
|
||||||
Assert.Throws<ArgumentNullException>(() => feature.ApplicationServices = null);
|
|
||||||
return context.Response.WriteAsync("Success");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
string result = await server.CreateClient().GetStringAsync("/path");
|
|
||||||
Assert.Equal("Success", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task CanSetCustomServiceProvider()
|
public async Task CanSetCustomServiceProvider()
|
||||||
{
|
{
|
||||||
|
|
@ -147,16 +131,11 @@ namespace Microsoft.AspNet.TestHost
|
||||||
{
|
{
|
||||||
app.Run(context =>
|
app.Run(context =>
|
||||||
{
|
{
|
||||||
context.ApplicationServices = new ServiceCollection()
|
|
||||||
.AddTransient<TestService>()
|
|
||||||
.BuildServiceProvider();
|
|
||||||
|
|
||||||
context.RequestServices = new ServiceCollection()
|
context.RequestServices = new ServiceCollection()
|
||||||
.AddTransient<TestService>()
|
.AddTransient<TestService>()
|
||||||
.BuildServiceProvider();
|
.BuildServiceProvider();
|
||||||
|
|
||||||
var s1 = context.ApplicationServices.GetRequiredService<TestService>();
|
var s = context.RequestServices.GetRequiredService<TestService>();
|
||||||
var s2 = context.RequestServices.GetRequiredService<TestService>();
|
|
||||||
|
|
||||||
return context.Response.WriteAsync("Success");
|
return context.Response.WriteAsync("Success");
|
||||||
});
|
});
|
||||||
|
|
@ -199,7 +178,6 @@ namespace Microsoft.AspNet.TestHost
|
||||||
{
|
{
|
||||||
app.Run(context =>
|
app.Run(context =>
|
||||||
{
|
{
|
||||||
Assert.Equal(appServices, context.ApplicationServices);
|
|
||||||
Assert.Equal(appServices, context.RequestServices);
|
Assert.Equal(appServices, context.RequestServices);
|
||||||
return context.Response.WriteAsync("Success");
|
return context.Response.WriteAsync("Success");
|
||||||
});
|
});
|
||||||
|
|
@ -236,7 +214,6 @@ namespace Microsoft.AspNet.TestHost
|
||||||
{
|
{
|
||||||
app.Run(context =>
|
app.Run(context =>
|
||||||
{
|
{
|
||||||
Assert.NotNull(context.ApplicationServices);
|
|
||||||
Assert.NotNull(context.RequestServices);
|
Assert.NotNull(context.RequestServices);
|
||||||
return context.Response.WriteAsync("Success");
|
return context.Response.WriteAsync("Success");
|
||||||
});
|
});
|
||||||
|
|
@ -246,31 +223,6 @@ namespace Microsoft.AspNet.TestHost
|
||||||
Assert.Equal("Success", result);
|
Assert.Equal("Success", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class EnsureApplicationServicesFilter : IStartupFilter
|
|
||||||
{
|
|
||||||
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
|
|
||||||
{
|
|
||||||
return builder =>
|
|
||||||
{
|
|
||||||
builder.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