Remove ObjectPoolProvider from default set of hosting services (#6437)

- Added it where it was being used
- Updated tests
This commit is contained in:
David Fowler 2019-01-08 12:05:48 -08:00 committed by GitHub
parent 3b1a89c1fc
commit d116ab8550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 22 additions and 58 deletions

View File

@ -39,6 +39,8 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddSingleton<IAntiforgeryTokenStore, DefaultAntiforgeryTokenStore>(); services.TryAddSingleton<IAntiforgeryTokenStore, DefaultAntiforgeryTokenStore>();
services.TryAddSingleton<IClaimUidExtractor, DefaultClaimUidExtractor>(); services.TryAddSingleton<IClaimUidExtractor, DefaultClaimUidExtractor>();
services.TryAddSingleton<IAntiforgeryAdditionalDataProvider, DefaultAntiforgeryAdditionalDataProvider>(); services.TryAddSingleton<IAntiforgeryAdditionalDataProvider, DefaultAntiforgeryAdditionalDataProvider>();
services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.TryAddSingleton<ObjectPool<AntiforgerySerializationContext>>(serviceProvider => services.TryAddSingleton<ObjectPool<AntiforgerySerializationContext>>(serviceProvider =>
{ {

View File

@ -96,9 +96,6 @@ namespace Microsoft.AspNetCore.Hosting.Internal
// Conjure up a RequestServices // Conjure up a RequestServices
services.TryAddTransient<IStartupFilter, AutoRequestServicesStartupFilter>(); services.TryAddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
// Ensure object pooling is available everywhere.
services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
// Support UseStartup(assemblyName) // Support UseStartup(assemblyName)
if (!string.IsNullOrEmpty(webHostOptions.StartupAssembly)) if (!string.IsNullOrEmpty(webHostOptions.StartupAssembly))
{ {

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -281,9 +281,6 @@ namespace Microsoft.AspNetCore.Hosting
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>(); services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();
services.AddTransient<IServiceProviderFactory<IServiceCollection>, DefaultServiceProviderFactory>(); services.AddTransient<IServiceProviderFactory<IServiceCollection>, DefaultServiceProviderFactory>();
// Ensure object pooling is available everywhere.
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
if (!string.IsNullOrEmpty(_options.StartupAssembly)) if (!string.IsNullOrEmpty(_options.StartupAssembly))
{ {
try try

View File

@ -113,22 +113,6 @@ namespace Microsoft.AspNetCore.Hosting
} }
} }
[Theory]
[MemberData(nameof(DefaultWebHostBuildersWithConfig))]
public async Task DefaultObjectPoolProvider_IsRegistered(IWebHostBuilder builder)
{
var server = new TestServer();
var host = builder
.UseServer(server)
.Configure(app => { })
.Build();
using (host)
{
await host.StartAsync();
Assert.IsType<DefaultObjectPoolProvider>(host.Services.GetService<ObjectPoolProvider>());
}
}
[Theory] [Theory]
[MemberData(nameof(DefaultWebHostBuildersWithConfig))] [MemberData(nameof(DefaultWebHostBuildersWithConfig))]
public async Task StartupConfigureServicesThrows_Fallback(IWebHostBuilder builder) public async Task StartupConfigureServicesThrows_Fallback(IWebHostBuilder builder)

3
src/Hosting/startvs.cmd Normal file
View File

@ -0,0 +1,3 @@
@ECHO OFF
%~dp0..\..\startvs.cmd %~dp0Hosting.sln

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
@ -32,7 +32,6 @@ namespace Microsoft.AspNetCore.Routing
private protected IServiceProvider CreateServices() private protected IServiceProvider CreateServices()
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddLogging(); services.AddLogging();
services.AddOptions(); services.AddOptions();
services.AddRouting(); services.AddRouting();

View File

@ -33,6 +33,7 @@ namespace Microsoft.Extensions.DependencyInjection
} }
services.TryAddTransient<IInlineConstraintResolver, DefaultInlineConstraintResolver>(); services.TryAddTransient<IInlineConstraintResolver, DefaultInlineConstraintResolver>();
services.TryAddTransient<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.TryAddSingleton<ObjectPool<UriBuildingContext>>(s => services.TryAddSingleton<ObjectPool<UriBuildingContext>>(s =>
{ {
var provider = s.GetRequiredService<ObjectPoolProvider>(); var provider = s.GetRequiredService<ObjectPoolProvider>();

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -32,7 +32,6 @@ namespace Microsoft.AspNetCore.Routing
protected ServiceCollection GetBasicServices() protected ServiceCollection GetBasicServices()
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddOptions(); services.AddOptions();
services.AddRouting(); services.AddRouting();
services.AddLogging(); services.AddLogging();

View File

@ -127,7 +127,6 @@ namespace Microsoft.AspNetCore.Routing
private static IServiceProvider CreateServices() private static IServiceProvider CreateServices()
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddOptions(); services.AddOptions();
services.AddRouting(); services.AddRouting();
services.AddLogging(); services.AddLogging();

View File

@ -606,7 +606,6 @@ namespace Microsoft.AspNetCore.Routing
var request = new Mock<HttpRequest>(MockBehavior.Strict); var request = new Mock<HttpRequest>(MockBehavior.Strict);
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddOptions(); services.AddOptions();
services.AddRouting(); services.AddRouting();
if (options != null) if (options != null)
@ -628,7 +627,6 @@ namespace Microsoft.AspNetCore.Routing
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance); services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddOptions(); services.AddOptions();
services.AddRouting(); services.AddRouting();
if (options != null) if (options != null)

View File

@ -654,11 +654,10 @@ namespace Microsoft.AspNetCore.Routing
public void GetVirtualPath_AlwaysUsesDefaultUrlEncoder() public void GetVirtualPath_AlwaysUsesDefaultUrlEncoder()
{ {
// Arrange // Arrange
var nameRouteValue = "name with %special #characters Jörn"; var nameRouteValue = "name with %special #characters Jörn";
var expected = "/Home/Index?name=" + UrlEncoder.Default.Encode(nameRouteValue); var expected = "/Home/Index?name=" + UrlEncoder.Default.Encode(nameRouteValue);
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance); services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddRouting(); services.AddRouting();
// This test encoder should not be used by Routing and should always use the default one. // This test encoder should not be used by Routing and should always use the default one.
services.AddSingleton<UrlEncoder>(new UrlTestEncoder()); services.AddSingleton<UrlEncoder>(new UrlTestEncoder());
@ -1521,7 +1520,6 @@ namespace Microsoft.AspNetCore.Routing
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance); services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance);
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddRouting(); services.AddRouting();
var context = new DefaultHttpContext var context = new DefaultHttpContext

View File

@ -1,10 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNetCore.ResponseCaching; using Microsoft.AspNetCore.ResponseCaching;
using Microsoft.AspNetCore.ResponseCaching.Internal; using Microsoft.AspNetCore.ResponseCaching.Internal;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.ObjectPool;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
@ -25,8 +26,9 @@ namespace Microsoft.Extensions.DependencyInjection
throw new ArgumentNullException(nameof(services)); throw new ArgumentNullException(nameof(services));
} }
services.TryAdd(ServiceDescriptor.Singleton<IResponseCachingPolicyProvider, ResponseCachingPolicyProvider>()); services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.TryAdd(ServiceDescriptor.Singleton<IResponseCachingKeyProvider, ResponseCachingKeyProvider>()); services.TryAddSingleton<IResponseCachingPolicyProvider, ResponseCachingPolicyProvider>();
services.TryAddSingleton<IResponseCachingKeyProvider, ResponseCachingKeyProvider>();
return services; return services;
} }

View File

@ -28,7 +28,6 @@ namespace MusicStore.Controllers
var services = new ServiceCollection(); var services = new ServiceCollection();
services services
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase("Scratch").UseInternalServiceProvider(efServiceProvider)); .AddDbContext<MusicStoreContext>(b => b.UseInMemoryDatabase("Scratch").UseInternalServiceProvider(efServiceProvider));
services.AddMvc(); services.AddMvc();

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Formatters.Json; using Microsoft.AspNetCore.Mvc.Formatters.Json;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -73,6 +74,7 @@ namespace Microsoft.Extensions.DependencyInjection
// Internal for testing. // Internal for testing.
internal static void AddJsonFormatterServices(IServiceCollection services) internal static void AddJsonFormatterServices(IServiceCollection services)
{ {
services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcJsonMvcOptionsSetup>()); ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, MvcJsonMvcOptionsSetup>());
services.TryAddEnumerable( services.TryAddEnumerable(

View File

@ -772,7 +772,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
private static TreeRouteBuilder CreateBuilder() private static TreeRouteBuilder CreateBuilder()
{ {
var services = new ServiceCollection() var services = new ServiceCollection()
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance)
.AddLogging() .AddLogging()
.AddRouting() .AddRouting()
@ -815,7 +814,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
IActionDescriptorCollectionProvider actionDescriptorProvider) IActionDescriptorCollectionProvider actionDescriptorProvider)
{ {
var services = new ServiceCollection() var services = new ServiceCollection()
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance)
.AddLogging() .AddLogging()
.AddRouting() .AddRouting()

View File

@ -179,10 +179,8 @@ namespace Microsoft.AspNetCore.Mvc.Routing
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
var services = new ServiceCollection() var services = new ServiceCollection()
.AddSingleton<IInlineConstraintResolver>(inlineConstraintResolver) .AddSingleton<IInlineConstraintResolver>(inlineConstraintResolver);
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.AddRouting(); services.AddRouting();
services.AddOptions(); services.AddOptions();
services.AddLogging(); services.AddLogging();

View File

@ -195,7 +195,6 @@ namespace Microsoft.AspNetCore.Routing
services.AddLogging(); services.AddLogging();
services.AddRouting(); services.AddRouting();
services services
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<UrlEncoder>(UrlEncoder.Default); .AddSingleton<UrlEncoder>(UrlEncoder.Default);
services.TryAddEnumerable(ServiceDescriptor.Singleton<EndpointDataSource>(new DefaultEndpointDataSource(endpoints))); services.TryAddEnumerable(ServiceDescriptor.Singleton<EndpointDataSource>(new DefaultEndpointDataSource(endpoints)));
return services.BuildServiceProvider(); return services.BuildServiceProvider();

View File

@ -193,7 +193,6 @@ namespace Microsoft.AspNetCore.Routing
services.AddLogging(); services.AddLogging();
services.AddRouting(); services.AddRouting();
services services
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<UrlEncoder>(UrlEncoder.Default); .AddSingleton<UrlEncoder>(UrlEncoder.Default);
services.TryAddEnumerable(ServiceDescriptor.Singleton<EndpointDataSource>(new DefaultEndpointDataSource(endpoints))); services.TryAddEnumerable(ServiceDescriptor.Singleton<EndpointDataSource>(new DefaultEndpointDataSource(endpoints)));
return services.BuildServiceProvider(); return services.BuildServiceProvider();

View File

@ -109,7 +109,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
services.AddLogging(); services.AddLogging();
services.AddRouting(); services.AddRouting();
services services
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<UrlEncoder>(UrlEncoder.Default); .AddSingleton<UrlEncoder>(UrlEncoder.Default);
return services.BuildServiceProvider(); return services.BuildServiceProvider();

View File

@ -1018,7 +1018,6 @@ namespace Microsoft.AspNetCore.Mvc.Routing
services.AddLogging(); services.AddLogging();
services.AddRouting(); services.AddRouting();
services services
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<UrlEncoder>(UrlEncoder.Default); .AddSingleton<UrlEncoder>(UrlEncoder.Default);
return services; return services;
} }

View File

@ -117,8 +117,7 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
serviceCollection.AddMvc(); serviceCollection.AddMvc();
serviceCollection serviceCollection
.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance)
.AddTransient<ILogger<DefaultAuthorizationService>, Logger<DefaultAuthorizationService>>() .AddTransient<ILogger<DefaultAuthorizationService>, Logger<DefaultAuthorizationService>>();
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
return serviceCollection.BuildServiceProvider(); return serviceCollection.BuildServiceProvider();
} }

View File

@ -21,7 +21,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Arrange // Arrange
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(); serviceCollection.AddLogging();
serviceCollection.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
serviceCollection.AddMvc(); serviceCollection.AddMvc();
var services = serviceCollection.BuildServiceProvider(); var services = serviceCollection.BuildServiceProvider();
@ -37,7 +36,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
// Arrange // Arrange
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(); serviceCollection.AddLogging();
serviceCollection.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
serviceCollection.AddMvc(mvcOptions => serviceCollection.AddMvc(mvcOptions =>
{ {
mvcOptions.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(IQueryable))); mvcOptions.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(IQueryable)));

View File

@ -193,7 +193,6 @@ namespace Microsoft.AspNetCore.Mvc.IntegrationTests
serviceCollection.AddMvc(); serviceCollection.AddMvc();
serviceCollection serviceCollection
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance) .AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance)
.AddTransient<ILogger<DefaultAuthorizationService>, Logger<DefaultAuthorizationService>>(); .AddTransient<ILogger<DefaultAuthorizationService>, Logger<DefaultAuthorizationService>>();

View File

@ -269,7 +269,6 @@ namespace Microsoft.AspNetCore.Mvc
serviceCollection.AddSingleton<DiagnosticListener>(diagnosticListener); serviceCollection.AddSingleton<DiagnosticListener>(diagnosticListener);
serviceCollection.AddMvc(); serviceCollection.AddMvc();
serviceCollection serviceCollection
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddTransient<ILoggerFactory, LoggerFactory>(); .AddTransient<ILoggerFactory, LoggerFactory>();
if (action != null) if (action != null)

View File

@ -256,7 +256,6 @@ namespace Microsoft.AspNetCore.Mvc
var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddSingleton<IHostingEnvironment>(GetHostingEnvironment()); services.AddSingleton<IHostingEnvironment>(GetHostingEnvironment());
services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
var diagnosticListener = new DiagnosticListener("Microsoft.AspNet"); var diagnosticListener = new DiagnosticListener("Microsoft.AspNet");
services.AddSingleton<DiagnosticSource>(diagnosticListener); services.AddSingleton<DiagnosticSource>(diagnosticListener);

View File

@ -187,7 +187,6 @@ namespace Microsoft.AspNetCore.Mvc
{ {
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection serviceCollection
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<ILoggerFactory>(new NullLoggerFactory()); .AddSingleton<ILoggerFactory>(new NullLoggerFactory());
serviceCollection.AddOptions(); serviceCollection.AddOptions();

View File

@ -489,7 +489,6 @@ namespace Microsoft.AspNetCore.Mvc
{ {
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
serviceCollection serviceCollection
.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>()
.AddSingleton<ILoggerFactory>(new NullLoggerFactory()); .AddSingleton<ILoggerFactory>(new NullLoggerFactory());
serviceCollection.AddOptions(); serviceCollection.AddOptions();