-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathWebApplicationBuilder.cs
More file actions
404 lines (336 loc) · 16.3 KB
/
Copy pathWebApplicationBuilder.cs
File metadata and controls
404 lines (336 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.StaticWebAssets;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// A builder for web applications and services.
/// </summary>
public class WebApplicationBuilder
{
private readonly HostBuilder _hostBuilder = new HostBuilder();
private readonly DeferredHostBuilder _deferredHostBuilder;
private readonly DeferredWebHostBuilder _deferredWebHostBuilder;
/// <summary>
/// Creates a <see cref="WebApplicationBuilder"/>.
/// </summary>
public WebApplicationBuilder() : this(callingAssembly: null, b => { })
{
}
internal WebApplicationBuilder(Assembly callingAssembly, Action<IHostBuilder> configureHost)
{
Services = new ServiceCollection();
// HACK: MVC and Identity do this horrible thing to get the hosting environment as an instance
// from the service collection before it is built. That needs to be fixed...
var environment = new WebHostEnvironment(callingAssembly);
Environment = environment;
Services.AddSingleton(Environment);
Configuration = new Configuration();
// Run this inline to populate the configuration
configureHost(new ConfigurationHostBuilder(Configuration, Environment));
Configuration.SetBasePath(environment.ContentRootPath);
Logging = new LoggingBuilder(Services);
Server = _deferredWebHostBuilder = new DeferredWebHostBuilder(Configuration, environment, Services);
Host = _deferredHostBuilder = new DeferredHostBuilder(Configuration, configureHost, environment, Services);
}
/// <summary>
/// Provides information about the web hosting environment an application is running.
/// </summary>
public IWebHostEnvironment Environment { get; }
/// <summary>
/// A collection of services for the application to compose. This is useful for adding user provided or framework provided services.
/// </summary>
public IServiceCollection Services { get; }
/// <summary>
/// A collection of configuration providers for the application to compose. This is useful for adding new configuration sources and providers.
/// </summary>
public Configuration Configuration { get; }
/// <summary>
/// A collection of logging providers for the applicaiton to compose. This is useful for adding new logging providers.
/// </summary>
public ILoggingBuilder Logging { get; }
/// <summary>
/// A builder for configuring server specific properties.
/// </summary>
public IWebHostBuilder Server { get; }
/// <summary>
/// A builder for configure host specific properties.
/// </summary>
public IHostBuilder Host { get; }
/// <summary>
/// Builds the <see cref="WebApplication"/>.
/// </summary>
/// <returns>A configured <see cref="WebApplication"/>.</returns>
public WebApplication Build()
{
WebApplication sourcePipeline = null;
_hostBuilder.ConfigureServices(services =>
{
foreach (var s in Services)
{
services.Add(s);
}
});
_hostBuilder.ConfigureAppConfiguration((hostContext, builder) =>
{
foreach (var s in Configuration.Sources)
{
builder.Sources.Add(s);
}
});
_deferredHostBuilder.ExecuteActions(_hostBuilder);
_hostBuilder.ConfigureWebHostDefaults(web =>
{
web.Configure(destinationPipeline =>
{
// The endpoints were already added on the outside
if (sourcePipeline.DataSources.Count > 0)
{
// The user did not register the routing middleware so wrap the entire
// destination pipeline in UseRouting() and UseEndpoints(), essentially:
// destination.UseRouting()
// destination.Run(source)
// destination.UseEndpoints()
if (sourcePipeline.RouteBuilder == null)
{
destinationPipeline.UseRouting();
// Copy the route data sources over to the destination pipeline, this should be available since we just called
// UseRouting()
var routes = (IEndpointRouteBuilder)destinationPipeline.Properties[WebApplication.EndpointRouteBuilder];
foreach (var ds in sourcePipeline.DataSources)
{
routes.DataSources.Add(ds);
}
// Chain the execution of the source pipeline into the destination pipeline
destinationPipeline.Use(next =>
{
sourcePipeline.Run(next);
return sourcePipeline.Build();
});
// Add a UseEndpoints at the end
destinationPipeline.UseEndpoints(e => { });
}
else
{
// Since we register routes into the source pipeline's route builder directly,
// if the user called UseRouting, we need to copy the data sources
foreach (var ds in sourcePipeline.DataSources)
{
sourcePipeline.RouteBuilder.DataSources.Add(ds);
}
// We then implicitly call UseEndpoints at the end of the pipeline
sourcePipeline.UseEndpoints(_ => { });
// Wire the source pipeline to run in the destination pipeline
destinationPipeline.Run(sourcePipeline.Build());
}
}
else
{
// Wire the source pipeline to run in the destination pipeline
destinationPipeline.Run(sourcePipeline.Build());
}
// Copy the properties to the destination app builder
foreach (var item in sourcePipeline.Properties)
{
destinationPipeline.Properties[item.Key] = item.Value;
}
});
// Make the default web host settings match and allow overrides
web.UseEnvironment(Environment.EnvironmentName);
web.UseContentRoot(Environment.ContentRootPath);
web.UseSetting(WebHostDefaults.ApplicationKey, Environment.ApplicationName);
web.UseSetting(WebHostDefaults.WebRootKey, Environment.WebRootPath);
_deferredWebHostBuilder.ExecuteActions(web);
});
var host = _hostBuilder.Build();
return sourcePipeline = new WebApplication(host);
}
private class DeferredHostBuilder : IHostBuilder
{
private Action<IHostBuilder> _operations;
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
private readonly IConfigurationBuilder _hostConfiguration = new ConfigurationBuilder();
private readonly WebHostEnvironment _environment;
private readonly Configuration _configuration;
private readonly IServiceCollection _services;
public DeferredHostBuilder(Configuration configuration, Action<IHostBuilder> configureHost, WebHostEnvironment environment, IServiceCollection services)
{
_configuration = configuration;
_environment = environment;
_services = services;
configureHost(this);
}
public IHost Build()
{
return null;
}
public IHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate)
{
_operations += b => b.ConfigureAppConfiguration(configureDelegate);
return this;
}
public IHostBuilder ConfigureContainer<TContainerBuilder>(Action<HostBuilderContext, TContainerBuilder> configureDelegate)
{
_operations += b => b.ConfigureContainer(configureDelegate);
return this;
}
public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> configureDelegate)
{
// HACK: We need to evaluate the host configuration as they are changes so that we have an accurate view of the world
configureDelegate(_hostConfiguration);
var config = _hostConfiguration.Build();
_environment.ApplicationName = config[HostDefaults.ApplicationKey] ?? _environment.ApplicationName;
_environment.ContentRootPath = config[HostDefaults.ContentRootKey] ?? _environment.ContentRootPath;
_environment.EnvironmentName = config[HostDefaults.EnvironmentKey] ?? _environment.EnvironmentName;
_environment.ResolveFileProviders(config);
_configuration.ChangeBasePath(_environment.ContentRootPath);
_operations += b => b.ConfigureHostConfiguration(configureDelegate);
return this;
}
public IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate)
{
// Run these immediately so that they are observable by the imperative code
configureDelegate(new HostBuilderContext(Properties)
{
Configuration = _configuration,
HostingEnvironment = _environment
},
_services);
return this;
}
public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory)
{
_operations += b => b.UseServiceProviderFactory(factory);
return this;
}
public IHostBuilder UseServiceProviderFactory<TContainerBuilder>(Func<HostBuilderContext, IServiceProviderFactory<TContainerBuilder>> factory)
{
_operations += b => b.UseServiceProviderFactory(factory);
return this;
}
public void ExecuteActions(IHostBuilder hostBuilder)
{
_operations?.Invoke(hostBuilder);
}
}
private class DeferredWebHostBuilder : IWebHostBuilder
{
private Action<IWebHostBuilder> _operations;
private readonly WebHostEnvironment _environment;
private readonly Configuration _configuration;
private readonly Dictionary<string, string> _settings = new Dictionary<string, string>();
private readonly IServiceCollection _services;
public DeferredWebHostBuilder(Configuration configuration, WebHostEnvironment environment, IServiceCollection services)
{
_configuration = configuration;
_environment = environment;
_services = services;
}
IWebHost IWebHostBuilder.Build()
{
return null;
}
public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
{
_operations += b => b.ConfigureAppConfiguration(configureDelegate);
return this;
}
public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices)
{
configureServices(new WebHostBuilderContext
{
Configuration = _configuration,
HostingEnvironment = _environment
},
_services);
return this;
}
public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices)
{
return ConfigureServices((WebHostBuilderContext context, IServiceCollection services) => configureServices(services));
}
public string GetSetting(string key)
{
_settings.TryGetValue(key, out var value);
return value;
}
public IWebHostBuilder UseSetting(string key, string value)
{
_settings[key] = value;
if (key == WebHostDefaults.ApplicationKey)
{
_environment.ApplicationName = value;
}
else if (key == WebHostDefaults.ContentRootKey)
{
_environment.ContentRootPath = value;
_environment.ResolveFileProviders(_configuration);
_configuration.ChangeBasePath(value);
}
else if (key == WebHostDefaults.EnvironmentKey)
{
_environment.EnvironmentName = value;
}
else if (key == WebHostDefaults.WebRootKey)
{
_environment.WebRootPath = value;
_environment.ResolveFileProviders(_configuration);
}
_operations += b => b.UseSetting(key, value);
return this;
}
public void ExecuteActions(IWebHostBuilder webHostBuilder)
{
_operations?.Invoke(webHostBuilder);
}
}
private class LoggingBuilder : ILoggingBuilder
{
public LoggingBuilder(IServiceCollection services)
{
Services = services;
}
public IServiceCollection Services { get; }
}
private class WebHostEnvironment : IWebHostEnvironment
{
public WebHostEnvironment(Assembly callingAssembly)
{
ContentRootPath = Directory.GetCurrentDirectory();
ApplicationName = (callingAssembly ?? Assembly.GetEntryAssembly()).GetName().Name;
EnvironmentName = Environments.Production;
// Default to /wwwroot if it exists.
var wwwroot = Path.Combine(ContentRootPath, "wwwroot");
if (Directory.Exists(wwwroot))
{
WebRootPath = wwwroot;
}
ResolveFileProviders(new Configuration());
}
public void ResolveFileProviders(IConfiguration configuration)
{
ContentRootFileProvider = Directory.Exists(ContentRootPath) ? (IFileProvider)new PhysicalFileProvider(ContentRootPath) : new NullFileProvider();
WebRootFileProvider = WebRootPath != null && Directory.Exists(Path.Combine(ContentRootPath, WebRootPath)) ? (IFileProvider)new PhysicalFileProvider(Path.Combine(ContentRootPath, WebRootPath)) : new NullFileProvider();
if (this.IsDevelopment())
{
StaticWebAssetsLoader.UseStaticWebAssets(this, configuration);
}
}
public IFileProvider WebRootFileProvider { get; set; }
public string WebRootPath { get; set; }
public string ApplicationName { get; set; }
public IFileProvider ContentRootFileProvider { get; set; }
public string ContentRootPath { get; set; }
public string EnvironmentName { get; set; }
}
}
}