Added a method to configure the web host defaults (#298)
- Added ConfigureWebHostDefaults to configure the web specific defaults on the generic host builder.
This commit is contained in:
parent
6bf1cc8545
commit
501dd3924b
|
|
@ -3,31 +3,34 @@
|
|||
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Label="Package Versions">
|
||||
<InternalAspNetCoreSdkPackageVersion>3.0.0-build-20181114.5</InternalAspNetCoreSdkPackageVersion>
|
||||
<MicrosoftAspNetCoreAspNetCoreModulePackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreAspNetCoreModulePackageVersion>
|
||||
<MicrosoftAspNetCoreAspNetCoreModuleV2PackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreAspNetCoreModuleV2PackageVersion>
|
||||
<MicrosoftAspNetCoreDiagnosticsPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreDiagnosticsPackageVersion>
|
||||
<MicrosoftAspNetCoreHostFilteringPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreHostFilteringPackageVersion>
|
||||
<MicrosoftAspNetCoreHostingPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreHostingPackageVersion>
|
||||
<MicrosoftAspNetCoreRoutingPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreRoutingPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIISIntegrationPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreServerIISIntegrationPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIISPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreServerIISPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIntegrationTestingIISPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreServerIntegrationTestingIISPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>0.7.0-alpha1-10742</MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>
|
||||
<MicrosoftAspNetCoreServerKestrelHttpsPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreServerKestrelHttpsPackageVersion>
|
||||
<MicrosoftAspNetCoreServerKestrelPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreServerKestrelPackageVersion>
|
||||
<MicrosoftAspNetCoreStaticFilesPackageVersion>3.0.0-alpha1-10742</MicrosoftAspNetCoreStaticFilesPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationCommandLinePackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsConfigurationCommandLinePackageVersion>
|
||||
<MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationFileExtensionsPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsConfigurationFileExtensionsPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationJsonPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsConfigurationJsonPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationUserSecretsPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsConfigurationUserSecretsPackageVersion>
|
||||
<MicrosoftExtensionsLoggingConfigurationPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsLoggingConfigurationPackageVersion>
|
||||
<MicrosoftExtensionsLoggingConsolePackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsLoggingConsolePackageVersion>
|
||||
<MicrosoftExtensionsLoggingDebugPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsLoggingDebugPackageVersion>
|
||||
<MicrosoftExtensionsLoggingEventSourcePackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsLoggingEventSourcePackageVersion>
|
||||
<MicrosoftExtensionsLoggingPackageVersion>3.0.0-preview-181113-11</MicrosoftExtensionsLoggingPackageVersion>
|
||||
<MicrosoftNETCoreAppPackageVersion>3.0.0-preview1-26907-05</MicrosoftNETCoreAppPackageVersion>
|
||||
<InternalAspNetCoreSdkPackageVersion>3.0.0-alpha1-20181004.7</InternalAspNetCoreSdkPackageVersion>
|
||||
<MicrosoftAspNetCoreAspNetCoreModulePackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreAspNetCoreModulePackageVersion>
|
||||
<MicrosoftAspNetCoreAspNetCoreModuleV2PackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreAspNetCoreModuleV2PackageVersion>
|
||||
<MicrosoftAspNetCoreDiagnosticsPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreDiagnosticsPackageVersion>
|
||||
<MicrosoftAspNetCoreHostFilteringPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreHostFilteringPackageVersion>
|
||||
<MicrosoftAspNetCoreHostingPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreHostingPackageVersion>
|
||||
<MicrosoftAspNetCoreRoutingPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreRoutingPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIISIntegrationPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreServerIISIntegrationPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIISPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreServerIISPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIntegrationTestingIISPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreServerIntegrationTestingIISPackageVersion>
|
||||
<MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>0.7.0-alpha1-10733</MicrosoftAspNetCoreServerIntegrationTestingPackageVersion>
|
||||
<MicrosoftAspNetCoreServerKestrelHttpsPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreServerKestrelHttpsPackageVersion>
|
||||
<MicrosoftAspNetCoreServerKestrelPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreServerKestrelPackageVersion>
|
||||
<MicrosoftAspNetCoreStaticFilesPackageVersion>3.0.0-alpha1-10733</MicrosoftAspNetCoreStaticFilesPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationCommandLinePackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsConfigurationCommandLinePackageVersion>
|
||||
<MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsConfigurationEnvironmentVariablesPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationFileExtensionsPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsConfigurationFileExtensionsPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationJsonPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsConfigurationJsonPackageVersion>
|
||||
<MicrosoftExtensionsConfigurationUserSecretsPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsConfigurationUserSecretsPackageVersion>
|
||||
<MicrosoftExtensionsLoggingConfigurationPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsLoggingConfigurationPackageVersion>
|
||||
<MicrosoftExtensionsLoggingConsolePackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsLoggingConsolePackageVersion>
|
||||
<MicrosoftExtensionsLoggingDebugPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsLoggingDebugPackageVersion>
|
||||
<MicrosoftExtensionsLoggingEventSourcePackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsLoggingEventSourcePackageVersion>
|
||||
<MicrosoftExtensionsLoggingPackageVersion>3.0.0-preview-181108-06</MicrosoftExtensionsLoggingPackageVersion>
|
||||
<MicrosoftExtensionsHostingPackageVersion>3.0.0-alpha1-10733</MicrosoftExtensionsHostingPackageVersion>
|
||||
<MicrosoftNETCoreApp20PackageVersion>2.0.9</MicrosoftNETCoreApp20PackageVersion>
|
||||
<MicrosoftNETCoreApp21PackageVersion>2.1.3</MicrosoftNETCoreApp21PackageVersion>
|
||||
<MicrosoftNETCoreApp22PackageVersion>2.2.0-rtm-27105-02</MicrosoftNETCoreApp22PackageVersion>
|
||||
<MicrosoftNETTestSdkPackageVersion>15.6.1</MicrosoftNETTestSdkPackageVersion>
|
||||
<MoqPackageVersion>4.10.0</MoqPackageVersion>
|
||||
<NETStandardLibrary20PackageVersion>2.0.3</NETStandardLibrary20PackageVersion>
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ using Microsoft.AspNetCore.Builder;
|
|||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace SampleApp
|
||||
{
|
||||
|
|
@ -23,6 +25,8 @@ namespace SampleApp
|
|||
CustomApplicationBuilder();
|
||||
|
||||
StartupClass(args);
|
||||
|
||||
HostBuilderWithWebHost(args);
|
||||
}
|
||||
|
||||
private static void HelloWorld()
|
||||
|
|
@ -90,5 +94,21 @@ namespace SampleApp
|
|||
host.Run();
|
||||
}
|
||||
}
|
||||
|
||||
private static void HostBuilderWithWebHost(string[] args)
|
||||
{
|
||||
var host = new HostBuilder()
|
||||
.ConfigureAppConfiguration(config =>
|
||||
{
|
||||
config.AddCommandLine(args);
|
||||
})
|
||||
.ConfigureWebHostDefaults(builder =>
|
||||
{
|
||||
builder.UseStartup<Startup>();
|
||||
})
|
||||
.Build();
|
||||
|
||||
host.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="$(MicrosoftAspNetCoreStaticFilesPackageVersion)" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsHostingPackageVersion)" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.AspNetCore\Microsoft.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore;
|
||||
|
||||
namespace Microsoft.Extensions.Hosting
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for configuring the IWebHostBuilder.
|
||||
/// </summary>
|
||||
public static class GenericHostBuilderExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="IWebHostBuilder"/> class with pre-configured defaults.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The following defaults are applied to the <see cref="IWebHostBuilder"/>:
|
||||
/// use Kestrel as the web server and configure it using the application's configuration providers,
|
||||
/// and enable IIS integration.
|
||||
/// </remarks>
|
||||
/// <param name="builder">The <see cref="IHostBuilder" /> instance to configure</param>
|
||||
/// <param name="configure">The configure callback</param>
|
||||
/// <returns></returns>
|
||||
public static IHostBuilder ConfigureWebHostDefaults(this IHostBuilder builder, Action<IWebHostBuilder> configure)
|
||||
{
|
||||
return builder.ConfigureWebHost(webHostBuilder =>
|
||||
{
|
||||
WebHost.ConfigureWebDefaults(webHostBuilder);
|
||||
|
||||
configure(webHostBuilder);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -158,69 +158,75 @@ namespace Microsoft.AspNetCore
|
|||
builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
|
||||
}
|
||||
|
||||
builder.UseKestrel((builderContext, options) =>
|
||||
{
|
||||
options.Configure(builderContext.Configuration.GetSection("Kestrel"));
|
||||
})
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
builder.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
|
||||
if (env.IsDevelopment())
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
|
||||
if (appAssembly != null)
|
||||
{
|
||||
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
|
||||
if (appAssembly != null)
|
||||
{
|
||||
config.AddUserSecrets(appAssembly, optional: true);
|
||||
}
|
||||
config.AddUserSecrets(appAssembly, optional: true);
|
||||
}
|
||||
}
|
||||
|
||||
config.AddEnvironmentVariables();
|
||||
config.AddEnvironmentVariables();
|
||||
|
||||
if (args != null)
|
||||
{
|
||||
config.AddCommandLine(args);
|
||||
}
|
||||
})
|
||||
.ConfigureLogging((hostingContext, logging) =>
|
||||
if (args != null)
|
||||
{
|
||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||
logging.AddConsole();
|
||||
logging.AddDebug();
|
||||
logging.AddEventSourceLogger();
|
||||
})
|
||||
.ConfigureServices((hostingContext, services) =>
|
||||
{
|
||||
// Fallback
|
||||
services.PostConfigure<HostFilteringOptions>(options =>
|
||||
{
|
||||
if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
|
||||
{
|
||||
// "AllowedHosts": "localhost;127.0.0.1;[::1]"
|
||||
var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
// Fall back to "*" to disable.
|
||||
options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
|
||||
}
|
||||
});
|
||||
// Change notification
|
||||
services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
|
||||
new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));
|
||||
config.AddCommandLine(args);
|
||||
}
|
||||
})
|
||||
.ConfigureLogging((hostingContext, logging) =>
|
||||
{
|
||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||
logging.AddConsole();
|
||||
logging.AddDebug();
|
||||
logging.AddEventSourceLogger();
|
||||
}).
|
||||
UseDefaultServiceProvider((context, options) =>
|
||||
{
|
||||
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
|
||||
});
|
||||
|
||||
services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
|
||||
})
|
||||
.UseIIS()
|
||||
.UseIISIntegration()
|
||||
.UseDefaultServiceProvider((context, options) =>
|
||||
{
|
||||
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
|
||||
});
|
||||
ConfigureWebDefaults(builder);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
internal static void ConfigureWebDefaults(IWebHostBuilder builder)
|
||||
{
|
||||
builder.UseKestrel((builderContext, options) =>
|
||||
{
|
||||
options.Configure(builderContext.Configuration.GetSection("Kestrel"));
|
||||
})
|
||||
.ConfigureServices((hostingContext, services) =>
|
||||
{
|
||||
// Fallback
|
||||
services.PostConfigure<HostFilteringOptions>(options =>
|
||||
{
|
||||
if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
|
||||
{
|
||||
// "AllowedHosts": "localhost;127.0.0.1;[::1]"
|
||||
var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
// Fall back to "*" to disable.
|
||||
options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
|
||||
}
|
||||
});
|
||||
// Change notification
|
||||
services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
|
||||
new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));
|
||||
|
||||
services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
|
||||
})
|
||||
.UseIIS()
|
||||
.UseIISIntegration();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class with pre-configured defaults using typed Startup.
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Reference in New Issue