From 73fd257844b6177680f1ccb3dcf3317caf813632 Mon Sep 17 00:00:00 2001 From: John Luo Date: Thu, 19 Nov 2015 09:35:23 -0800 Subject: [PATCH] Reacting to ApplicationServices removal from HttpContext --- .../Internal/HostingEngine.cs | 2 - .../RequestServicesContainerFeature.cs | 20 +------ .../TestServerTests.cs | 58 ++----------------- 3 files changed, 7 insertions(+), 73 deletions(-) diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs index 2a5ef5f534..4a796b6e6f 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs @@ -96,8 +96,6 @@ namespace Microsoft.AspNet.Hosting.Internal Server.Start( async httpContext => { - httpContext.ApplicationServices = _applicationServices; - if (diagnosticSource.IsEnabled("Microsoft.AspNet.Hosting.BeginRequest")) { diagnosticSource.Write("Microsoft.AspNet.Hosting.BeginRequest", new { httpContext = httpContext }); diff --git a/src/Microsoft.AspNet.Hosting/Internal/RequestServicesContainerFeature.cs b/src/Microsoft.AspNet.Hosting/Internal/RequestServicesContainerFeature.cs index 0de3d46ac6..455be554b4 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/RequestServicesContainerFeature.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/RequestServicesContainerFeature.cs @@ -21,23 +21,7 @@ namespace Microsoft.AspNet.Hosting.Internal throw new ArgumentNullException(nameof(applicationServices)); } - ApplicationServices = applicationServices; - } - - public IServiceProvider ApplicationServices - { - get - { - return _appServices; - } - set - { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - _appServices = value; - } + _appServices = applicationServices; } public IServiceProvider RequestServices @@ -46,7 +30,7 @@ namespace Microsoft.AspNet.Hosting.Internal { if (!_requestServicesSet) { - _scope = ApplicationServices.GetRequiredService().CreateScope(); + _scope = _appServices.GetRequiredService().CreateScope(); _requestServices = _scope.ServiceProvider; _requestServicesSet = true; } diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index d34bc4f447..3116e549fa 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -15,7 +15,6 @@ using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features.Internal; using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Testing.xunit; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DiagnosticAdapter; @@ -59,16 +58,17 @@ namespace Microsoft.AspNet.TestHost public void Configure(IApplicationBuilder app) { + var applicationServices = app.ApplicationServices; app.Run(async context => { - await context.Response.WriteAsync("ApplicationServicesEqual:" + (context.ApplicationServices == Services)); + await context.Response.WriteAsync("ApplicationServicesEqual:" + (applicationServices == Services)); }); } } [Fact] - public async Task CustomServiceProviderReplacesApplicationServices() + public async Task CustomServiceProviderSetsApplicationServices() { var server = new TestServer(TestServer.CreateBuilder().UseStartup()); string result = await server.CreateClient().GetStringAsync("/path"); @@ -124,22 +124,6 @@ namespace Microsoft.AspNet.TestHost Assert.Equal("Found:True", result); } - [Fact] - public async Task SettingApplicationServicesOnFeatureToNullThrows() - { - var server = TestServer.Create(app => - { - app.Run(context => - { - var feature = context.Features.Get(); - Assert.Throws(() => feature.ApplicationServices = null); - return context.Response.WriteAsync("Success"); - }); - }); - string result = await server.CreateClient().GetStringAsync("/path"); - Assert.Equal("Success", result); - } - [Fact] public async Task CanSetCustomServiceProvider() { @@ -147,16 +131,11 @@ namespace Microsoft.AspNet.TestHost { app.Run(context => { - context.ApplicationServices = new ServiceCollection() - .AddTransient() - .BuildServiceProvider(); - context.RequestServices = new ServiceCollection() .AddTransient() .BuildServiceProvider(); - - var s1 = context.ApplicationServices.GetRequiredService(); - var s2 = context.RequestServices.GetRequiredService(); + + var s = context.RequestServices.GetRequiredService(); return context.Response.WriteAsync("Success"); }); @@ -199,7 +178,6 @@ namespace Microsoft.AspNet.TestHost { app.Run(context => { - Assert.Equal(appServices, context.ApplicationServices); Assert.Equal(appServices, context.RequestServices); return context.Response.WriteAsync("Success"); }); @@ -236,7 +214,6 @@ namespace Microsoft.AspNet.TestHost { app.Run(context => { - Assert.NotNull(context.ApplicationServices); Assert.NotNull(context.RequestServices); return context.Response.WriteAsync("Success"); }); @@ -246,31 +223,6 @@ namespace Microsoft.AspNet.TestHost Assert.Equal("Success", result); } - - public class EnsureApplicationServicesFilter : IStartupFilter - { - public Action Configure(Action 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()); - string result = await server.CreateClient().GetStringAsync("/path"); - Assert.Equal("Done", result); - } - - [Fact] public async Task CanAccessLogger() {