Skip to content

Commit ef62c6d

Browse files
committed
Makes changes for the latest code.
Refactor the WebHostBuilder classes to share more code.
1 parent 743349d commit ef62c6d

File tree

5 files changed

+113
-195
lines changed

5 files changed

+113
-195
lines changed

src/DefaultBuilder/src/WebApplicationBuilder.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ internal WebApplicationBuilder(WebApplicationOptions options, bool slim, Action<
9292

9393
var configuration = new ConfigurationManager();
9494

95-
// TODO: Remove when DI no longer has RequiresDynamicCodeAttribute https://github.com/dotnet/runtime/pull/79425
96-
#pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
9795
_hostApplicationBuilder = new HostApplicationBuilder(new HostApplicationBuilderSettings
9896
{
9997
Args = options.Args,
@@ -102,7 +100,6 @@ internal WebApplicationBuilder(WebApplicationOptions options, bool slim, Action<
102100
ContentRootPath = options.ContentRootPath,
103101
Configuration = configuration,
104102
});
105-
#pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
106103

107104
// Set WebRootPath if necessary
108105
if (options.WebRootPath is not null)

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,17 @@
1818

1919
namespace Microsoft.AspNetCore.Hosting;
2020

21-
internal sealed class GenericWebHostBuilder : IWebHostBuilder, ISupportsStartup, ISupportsUseDefaultServiceProvider
21+
internal sealed class GenericWebHostBuilder : WebHostBuilderBase, ISupportsStartup
2222
{
23-
private readonly IHostBuilder _builder;
24-
private readonly IConfiguration _config;
2523
private object? _startupObject;
2624
private readonly object _startupKey = new object();
2725

2826
private AggregateException? _hostingStartupErrors;
2927
private HostingStartupWebHostBuilder? _hostingStartupWebHostBuilder;
3028

3129
public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options)
30+
: base(builder, options)
3231
{
33-
_builder = builder;
34-
var configBuilder = new ConfigurationBuilder()
35-
.AddInMemoryCollection();
36-
37-
if (!options.SuppressEnvironmentConfiguration)
38-
{
39-
configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
40-
}
41-
42-
_config = configBuilder.Build();
43-
4432
_builder.ConfigureHostConfiguration(config =>
4533
{
4634
config.AddConfiguration(_config);
@@ -172,51 +160,6 @@ private void ExecuteHostingStartups()
172160
}
173161
}
174162

175-
public IWebHost Build()
176-
{
177-
throw new NotSupportedException($"Building this implementation of {nameof(IWebHostBuilder)} is not supported.");
178-
}
179-
180-
public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
181-
{
182-
_builder.ConfigureAppConfiguration((context, builder) =>
183-
{
184-
var webhostBuilderContext = GetWebHostBuilderContext(context);
185-
configureDelegate(webhostBuilderContext, builder);
186-
});
187-
188-
return this;
189-
}
190-
191-
public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices)
192-
{
193-
return ConfigureServices((context, services) => configureServices(services));
194-
}
195-
196-
public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices)
197-
{
198-
_builder.ConfigureServices((context, builder) =>
199-
{
200-
var webhostBuilderContext = GetWebHostBuilderContext(context);
201-
configureServices(webhostBuilderContext, builder);
202-
});
203-
204-
return this;
205-
}
206-
207-
public IWebHostBuilder UseDefaultServiceProvider(Action<WebHostBuilderContext, ServiceProviderOptions> configure)
208-
{
209-
_builder.UseServiceProviderFactory(context =>
210-
{
211-
var webHostBuilderContext = GetWebHostBuilderContext(context);
212-
var options = new ServiceProviderOptions();
213-
configure(webHostBuilderContext, options);
214-
return new DefaultServiceProviderFactory(options);
215-
});
216-
217-
return this;
218-
}
219-
220163
public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptions.Accessibility)] Type startupType)
221164
{
222165
var startupAssemblyName = startupType.Assembly.GetName().Name;
@@ -412,40 +355,6 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
412355
return this;
413356
}
414357

415-
private WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext context)
416-
{
417-
if (!context.Properties.TryGetValue(typeof(WebHostBuilderContext), out var contextVal))
418-
{
419-
// Use _config as a fallback for WebHostOptions in case the chained source was removed from the hosting IConfigurationBuilder.
420-
var options = new WebHostOptions(context.Configuration, fallbackConfiguration: _config, environment: context.HostingEnvironment);
421-
var webHostBuilderContext = new WebHostBuilderContext
422-
{
423-
Configuration = context.Configuration,
424-
HostingEnvironment = new HostingEnvironment(),
425-
};
426-
webHostBuilderContext.HostingEnvironment.Initialize(context.HostingEnvironment.ContentRootPath, options, baseEnvironment: context.HostingEnvironment);
427-
context.Properties[typeof(WebHostBuilderContext)] = webHostBuilderContext;
428-
context.Properties[typeof(WebHostOptions)] = options;
429-
return webHostBuilderContext;
430-
}
431-
432-
// Refresh config, it's periodically updated/replaced
433-
var webHostContext = (WebHostBuilderContext)contextVal;
434-
webHostContext.Configuration = context.Configuration;
435-
return webHostContext;
436-
}
437-
438-
public string? GetSetting(string key)
439-
{
440-
return _config[key];
441-
}
442-
443-
public IWebHostBuilder UseSetting(string key, string? value)
444-
{
445-
_config[key] = value;
446-
return this;
447-
}
448-
449358
// This exists just so that we can use ActivatorUtilities.CreateInstance on the Startup class
450359
private sealed class HostServiceProvider : IServiceProvider
451360
{

src/Hosting/Hosting/src/GenericHost/SlimWebHostBuilder.cs

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,11 @@
1515

1616
namespace Microsoft.AspNetCore.Hosting;
1717

18-
internal sealed class SlimWebHostBuilder : IWebHostBuilder, ISupportsStartup, ISupportsUseDefaultServiceProvider
18+
internal sealed class SlimWebHostBuilder : WebHostBuilderBase, ISupportsStartup
1919
{
20-
private readonly IHostBuilder _builder;
21-
private readonly IConfiguration _config;
22-
2320
public SlimWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options)
21+
: base(builder, options)
2422
{
25-
_builder = builder;
26-
var configBuilder = new ConfigurationBuilder()
27-
.AddInMemoryCollection();
28-
29-
if (!options.SuppressEnvironmentConfiguration)
30-
{
31-
configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
32-
}
33-
34-
_config = configBuilder.Build();
35-
3623
_builder.ConfigureHostConfiguration(config =>
3724
{
3825
config.AddConfiguration(_config);
@@ -69,54 +56,6 @@ public SlimWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options)
6956
});
7057
}
7158

72-
public IWebHost Build()
73-
{
74-
throw new NotSupportedException($"Building this implementation of {nameof(IWebHostBuilder)} is not supported.");
75-
}
76-
77-
public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
78-
{
79-
_builder.ConfigureAppConfiguration((context, builder) =>
80-
{
81-
var webhostBuilderContext = GetWebHostBuilderContext(context);
82-
configureDelegate(webhostBuilderContext, builder);
83-
});
84-
85-
return this;
86-
}
87-
88-
public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices)
89-
{
90-
return ConfigureServices((context, services) => configureServices(services));
91-
}
92-
93-
public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices)
94-
{
95-
_builder.ConfigureServices((context, builder) =>
96-
{
97-
var webhostBuilderContext = GetWebHostBuilderContext(context);
98-
configureServices(webhostBuilderContext, builder);
99-
});
100-
101-
return this;
102-
}
103-
104-
public IWebHostBuilder UseDefaultServiceProvider(Action<WebHostBuilderContext, ServiceProviderOptions> configure)
105-
{
106-
_builder.UseServiceProviderFactory(context =>
107-
{
108-
var webHostBuilderContext = GetWebHostBuilderContext(context);
109-
var options = new ServiceProviderOptions();
110-
configure(webHostBuilderContext, options);
111-
// TODO: Remove when DI no longer has RequiresDynamicCodeAttribute https://github.com/dotnet/runtime/pull/79425
112-
#pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
113-
return new DefaultServiceProviderFactory(options);
114-
#pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.
115-
});
116-
117-
return this;
118-
}
119-
12059
public IWebHostBuilder Configure(Action<IApplicationBuilder> configure)
12160
{
12261
throw new NotSupportedException();
@@ -149,38 +88,4 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
14988

15089
return this;
15190
}
152-
153-
private WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext context)
154-
{
155-
if (!context.Properties.TryGetValue(typeof(WebHostBuilderContext), out var contextVal))
156-
{
157-
// Use _config as a fallback for WebHostOptions in case the chained source was removed from the hosting IConfigurationBuilder.
158-
var options = new WebHostOptions(context.Configuration, fallbackConfiguration: _config, environment: context.HostingEnvironment);
159-
var webHostBuilderContext = new WebHostBuilderContext
160-
{
161-
Configuration = context.Configuration,
162-
HostingEnvironment = new HostingEnvironment(),
163-
};
164-
webHostBuilderContext.HostingEnvironment.Initialize(context.HostingEnvironment.ContentRootPath, options, baseEnvironment: context.HostingEnvironment);
165-
context.Properties[typeof(WebHostBuilderContext)] = webHostBuilderContext;
166-
context.Properties[typeof(WebHostOptions)] = options;
167-
return webHostBuilderContext;
168-
}
169-
170-
// Refresh config, it's periodically updated/replaced
171-
var webHostContext = (WebHostBuilderContext)contextVal;
172-
webHostContext.Configuration = context.Configuration;
173-
return webHostContext;
174-
}
175-
176-
public string? GetSetting(string key)
177-
{
178-
return _config[key];
179-
}
180-
181-
public IWebHostBuilder UseSetting(string key, string? value)
182-
{
183-
_config[key] = value;
184-
return this;
185-
}
18691
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
8+
namespace Microsoft.AspNetCore.Hosting;
9+
10+
internal abstract class WebHostBuilderBase : IWebHostBuilder, ISupportsUseDefaultServiceProvider
11+
{
12+
private protected readonly IHostBuilder _builder;
13+
private protected readonly IConfiguration _config;
14+
15+
public WebHostBuilderBase(IHostBuilder builder, WebHostBuilderOptions options)
16+
{
17+
_builder = builder;
18+
var configBuilder = new ConfigurationBuilder()
19+
.AddInMemoryCollection();
20+
21+
if (!options.SuppressEnvironmentConfiguration)
22+
{
23+
configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_");
24+
}
25+
26+
_config = configBuilder.Build();
27+
}
28+
29+
public IWebHost Build()
30+
{
31+
throw new NotSupportedException($"Building this implementation of {nameof(IWebHostBuilder)} is not supported.");
32+
}
33+
34+
public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, IConfigurationBuilder> configureDelegate)
35+
{
36+
_builder.ConfigureAppConfiguration((context, builder) =>
37+
{
38+
var webhostBuilderContext = GetWebHostBuilderContext(context);
39+
configureDelegate(webhostBuilderContext, builder);
40+
});
41+
42+
return this;
43+
}
44+
45+
public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureServices)
46+
{
47+
return ConfigureServices((context, services) => configureServices(services));
48+
}
49+
50+
public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices)
51+
{
52+
_builder.ConfigureServices((context, builder) =>
53+
{
54+
var webhostBuilderContext = GetWebHostBuilderContext(context);
55+
configureServices(webhostBuilderContext, builder);
56+
});
57+
58+
return this;
59+
}
60+
61+
public IWebHostBuilder UseDefaultServiceProvider(Action<WebHostBuilderContext, ServiceProviderOptions> configure)
62+
{
63+
_builder.UseServiceProviderFactory(context =>
64+
{
65+
var webHostBuilderContext = GetWebHostBuilderContext(context);
66+
var options = new ServiceProviderOptions();
67+
configure(webHostBuilderContext, options);
68+
return new DefaultServiceProviderFactory(options);
69+
});
70+
71+
return this;
72+
}
73+
74+
protected WebHostBuilderContext GetWebHostBuilderContext(HostBuilderContext context)
75+
{
76+
if (!context.Properties.TryGetValue(typeof(WebHostBuilderContext), out var contextVal))
77+
{
78+
// Use _config as a fallback for WebHostOptions in case the chained source was removed from the hosting IConfigurationBuilder.
79+
var options = new WebHostOptions(context.Configuration, fallbackConfiguration: _config, environment: context.HostingEnvironment);
80+
var webHostBuilderContext = new WebHostBuilderContext
81+
{
82+
Configuration = context.Configuration,
83+
HostingEnvironment = new HostingEnvironment(),
84+
};
85+
webHostBuilderContext.HostingEnvironment.Initialize(context.HostingEnvironment.ContentRootPath, options, baseEnvironment: context.HostingEnvironment);
86+
context.Properties[typeof(WebHostBuilderContext)] = webHostBuilderContext;
87+
context.Properties[typeof(WebHostOptions)] = options;
88+
return webHostBuilderContext;
89+
}
90+
91+
// Refresh config, it's periodically updated/replaced
92+
var webHostContext = (WebHostBuilderContext)contextVal;
93+
webHostContext.Configuration = context.Configuration;
94+
return webHostContext;
95+
}
96+
97+
public string? GetSetting(string key)
98+
{
99+
return _config[key];
100+
}
101+
102+
public IWebHostBuilder UseSetting(string key, string? value)
103+
{
104+
_config[key] = value;
105+
return this;
106+
}
107+
}

src/Hosting/Hosting/src/GenericHostWebHostBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IW
3636
{
3737
return ConfigureWebHost(
3838
builder,
39-
(hostBuilder, options) => new GenericWebHostBuilder(hostBuilder, options),
39+
static (hostBuilder, options) => new GenericWebHostBuilder(hostBuilder, options),
4040
configure,
4141
configureWebHostBuilder);
4242
}
@@ -52,7 +52,7 @@ public static IHostBuilder ConfigureSlimWebHost(this IHostBuilder builder, Actio
5252
{
5353
return ConfigureWebHost(
5454
builder,
55-
(hostBuilder, options) => new SlimWebHostBuilder(hostBuilder, options),
55+
static (hostBuilder, options) => new SlimWebHostBuilder(hostBuilder, options),
5656
configure,
5757
configureWebHostBuilder);
5858
}

0 commit comments

Comments
 (0)