Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Add ObjectPool<StringBuilder> to default services #652

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Builder;
using Microsoft.AspNetCore.Hosting.Internal;
Expand All @@ -17,6 +18,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.PlatformAbstractions;

namespace Microsoft.AspNetCore.Hosting
Expand All @@ -26,6 +28,11 @@ namespace Microsoft.AspNetCore.Hosting
/// </summary>
public class WebHostBuilder : IWebHostBuilder
{
// StringBuilderPooledObjectPolicy.MaximumRetainedCapacity's default is too small for general use and will
// cause useful StringBuilders to be thrown away in common scenarios e.g. serializing an antiforgery cookie.
// Instead of that value (4 kB), allow the few StringBuilders to grow to 1 MB.
private const int MaximumBuilderSize = 0x100000;

private readonly IHostingEnvironment _hostingEnvironment;
private readonly ILoggerFactory _loggerFactory;
private readonly List<Action<IServiceCollection>> _configureServicesDelegates;
Expand Down Expand Up @@ -135,7 +142,7 @@ public IWebHostBuilder Configure(Action<IApplicationBuilder> configureApp)
}

/// <summary>
/// Configure the provided <see cref="ILoggerFactory"/> which will be available as a hosting service.
/// Configure the provided <see cref="ILoggerFactory"/> which will be available as a hosting service.
/// </summary>
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
Expand All @@ -155,7 +162,7 @@ public IWebHost Build()

var appEnvironment = hostingContainer.GetRequiredService<IApplicationEnvironment>();
var startupLoader = hostingContainer.GetRequiredService<IStartupLoader>();

var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath);
var applicationName = ResolveApplicationName() ?? appEnvironment.ApplicationName;

Expand Down Expand Up @@ -198,6 +205,17 @@ private IServiceCollection BuildHostingServices()
services.AddSingleton<DiagnosticSource>(diagnosticSource);
services.AddSingleton<DiagnosticListener>(diagnosticSource);

services.AddSingleton<ObjectPool<StringBuilder>>(serviceProvider =>
{
var provider = new DefaultObjectPoolProvider();
var policy = new StringBuilderPooledObjectPolicy
{
MaximumRetainedCapacity = MaximumBuilderSize,
};

return provider.Create(policy);
});

// Conjure up a RequestServices
services.AddTransient<IStartupFilter, AutoRequestServicesStartupFilter>();

Expand Down
5 changes: 3 additions & 2 deletions src/Microsoft.AspNetCore.Hosting/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
"Microsoft.AspNetCore.Hosting.Server.Abstractions": "1.0.0-*",
"Microsoft.AspNetCore.Http": "1.0.0-*",
"Microsoft.AspNetCore.Http.Extensions": "1.0.0-*",
"Microsoft.Extensions.FileProviders.Physical": "1.0.0-*",
"Microsoft.Extensions.Options": "1.0.0-*",
"Microsoft.Extensions.Configuration": "1.0.0-*",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-*",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.FileProviders.Physical": "1.0.0-*",
"Microsoft.Extensions.Logging": "1.0.0-*",
"Microsoft.Extensions.ObjectPool": "1.0.0-*",
"Microsoft.Extensions.Options": "1.0.0-*",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",
"Microsoft.Extensions.TypeNameHelper.Sources": {
"version": "1.0.0-*",
Expand Down
26 changes: 23 additions & 3 deletions test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Fakes;
using Microsoft.AspNetCore.Hosting.Internal;
Expand All @@ -13,8 +14,8 @@
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.ObjectPool;
using Microsoft.Extensions.PlatformAbstractions;
using System.Reflection;
using Xunit;

namespace Microsoft.AspNetCore.Hosting
Expand Down Expand Up @@ -92,12 +93,31 @@ public async Task IApplicationLifetimeRegisteredEvenWhenStartupCtorThrows_Fallba
using (host)
{
host.Start();
var service = host.Services.GetServices<IApplicationLifetime>();
Assert.NotNull(service);
var services = host.Services.GetServices<IApplicationLifetime>();
Assert.NotNull(services);
Assert.NotEmpty(services);

await AssertResponseContains(server.RequestDelegate, "Exception from constructor");
}
}

[Fact]
public void ObjectPoolOfStringBuilderRegistered()
{
var server = new TestServer();
var host = CreateWebHostBuilder()
.UseServer(server)
.Configure(app => { })
.Build();
using (host)
{
host.Start();
var services = host.Services.GetServices<ObjectPool<StringBuilder>>();
Assert.NotNull(services);
Assert.NotEmpty(services);
}
}

[Fact]
public async Task StartupConfigureServicesThrows_Fallback()
{
Expand Down