6
6
using System . Diagnostics ;
7
7
using System . IO ;
8
8
using System . Reflection ;
9
+ using System . Text ;
9
10
using Microsoft . AspNetCore . Builder ;
10
11
using Microsoft . AspNetCore . Hosting . Builder ;
11
12
using Microsoft . AspNetCore . Hosting . Internal ;
17
18
using Microsoft . Extensions . DependencyInjection ;
18
19
using Microsoft . Extensions . DependencyInjection . Extensions ;
19
20
using Microsoft . Extensions . Logging ;
21
+ using Microsoft . Extensions . ObjectPool ;
20
22
using Microsoft . Extensions . PlatformAbstractions ;
21
23
22
24
namespace Microsoft . AspNetCore . Hosting
@@ -26,6 +28,11 @@ namespace Microsoft.AspNetCore.Hosting
26
28
/// </summary>
27
29
public class WebHostBuilder : IWebHostBuilder
28
30
{
31
+ // StringBuilderPooledObjectPolicy.MaximumRetainedCapacity's default is too small for general use and will
32
+ // cause useful StringBuilders to be thrown away in common scenarios e.g. serializing an antiforgery cookie.
33
+ // Instead of that value (4 kB), allow the few StringBuilders to grow to 1 MB.
34
+ private const int MaximumBuilderSize = 0x100000 ;
35
+
29
36
private readonly IHostingEnvironment _hostingEnvironment ;
30
37
private readonly ILoggerFactory _loggerFactory ;
31
38
private readonly List < Action < IServiceCollection > > _configureServicesDelegates ;
@@ -135,7 +142,7 @@ public IWebHostBuilder Configure(Action<IApplicationBuilder> configureApp)
135
142
}
136
143
137
144
/// <summary>
138
- /// Configure the provided <see cref="ILoggerFactory"/> which will be available as a hosting service.
145
+ /// Configure the provided <see cref="ILoggerFactory"/> which will be available as a hosting service.
139
146
/// </summary>
140
147
/// <param name="configureLogging">The delegate that configures the <see cref="ILoggerFactory"/>.</param>
141
148
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
@@ -155,7 +162,7 @@ public IWebHost Build()
155
162
156
163
var appEnvironment = hostingContainer . GetRequiredService < IApplicationEnvironment > ( ) ;
157
164
var startupLoader = hostingContainer . GetRequiredService < IStartupLoader > ( ) ;
158
-
165
+
159
166
var contentRootPath = ResolveContentRootPath ( _options . ContentRootPath , appEnvironment . ApplicationBasePath ) ;
160
167
var applicationName = ResolveApplicationName ( ) ?? appEnvironment . ApplicationName ;
161
168
@@ -198,6 +205,17 @@ private IServiceCollection BuildHostingServices()
198
205
services . AddSingleton < DiagnosticSource > ( diagnosticSource ) ;
199
206
services . AddSingleton < DiagnosticListener > ( diagnosticSource ) ;
200
207
208
+ services . AddSingleton < ObjectPool < StringBuilder > > ( serviceProvider =>
209
+ {
210
+ var provider = new DefaultObjectPoolProvider ( ) ;
211
+ var policy = new StringBuilderPooledObjectPolicy
212
+ {
213
+ MaximumRetainedCapacity = MaximumBuilderSize ,
214
+ } ;
215
+
216
+ return provider . Create ( policy ) ;
217
+ } ) ;
218
+
201
219
// Conjure up a RequestServices
202
220
services . AddTransient < IStartupFilter , AutoRequestServicesStartupFilter > ( ) ;
203
221
0 commit comments