Expose WebHostBuilderContext in UseKestrel #1334 (#2177)

This commit is contained in:
Chris Ross 2017-11-22 10:19:17 -08:00 committed by GitHub
parent afa4844230
commit 8e1da5d1f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 22 deletions

View File

@ -13,15 +13,6 @@ namespace Http2SampleApp
{
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
if (!ushort.TryParse(configuration["BASE_PORT"], NumberStyles.None, CultureInfo.InvariantCulture, out var basePort))
{
basePort = 5000;
}
var hostBuilder = new WebHostBuilder()
.ConfigureLogging((_, factory) =>
{
@ -29,8 +20,10 @@ namespace Http2SampleApp
factory.SetMinimumLevel(LogLevel.Trace);
factory.AddConsole();
})
.UseKestrel(options =>
.UseKestrel((context, options) =>
{
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
// Run callbacks on the transport thread
options.ApplicationSchedulingMode = SchedulingMode.Inline;

View File

@ -43,22 +43,15 @@ namespace SampleApp
Console.WriteLine("Unobserved exception: {0}", e.Exception);
};
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
if (!ushort.TryParse(configuration["BASE_PORT"], NumberStyles.None, CultureInfo.InvariantCulture, out var basePort))
{
basePort = 5000;
}
var hostBuilder = new WebHostBuilder()
.ConfigureLogging((_, factory) =>
{
factory.AddConsole();
})
.UseKestrel(options =>
.UseKestrel((context, options) =>
{
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
// Run callbacks on the transport thread
options.ApplicationSchedulingMode = SchedulingMode.Inline;

View File

@ -57,5 +57,31 @@ namespace Microsoft.AspNetCore.Hosting
services.Configure(options);
});
}
/// <summary>
/// Specify Kestrel as the server to be used by the web host.
/// </summary>
/// <param name="hostBuilder">
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
/// </param>
/// <param name="configureOptions">A callback to configure Kestrel options.</param>
/// <returns>
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder.
/// </returns>
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, KestrelServerOptions> configureOptions)
{
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
return hostBuilder.UseKestrel().ConfigureServices((context, services) =>
{
services.Configure<KestrelServerOptions>(options =>
{
configureOptions(context, options);
});
});
}
}
}

View File

@ -3,10 +3,10 @@
set -e
cd /publish
systemd-socket-activate -l 8080 -E BASE_PORT=7000 dotnet SampleApp.dll &
systemd-socket-activate -l 8080 -E ASPNETCORE_BASE_PORT=7000 dotnet SampleApp.dll &
socat TCP-LISTEN:8081,fork TCP-CONNECT:127.0.0.1:7000 &
socat TCP-LISTEN:8082,fork TCP-CONNECT:127.0.0.1:7001 &
systemd-socket-activate -l /tmp/activate-kestrel.sock -E BASE_PORT=7100 dotnet SampleApp.dll &
systemd-socket-activate -l /tmp/activate-kestrel.sock -E ASPNETCORE_BASE_PORT=7100 dotnet SampleApp.dll &
socat TCP-LISTEN:8083,fork UNIX-CLIENT:/tmp/activate-kestrel.sock &
socat TCP-LISTEN:8084,fork TCP-CONNECT:127.0.0.1:7100 &
socat TCP-LISTEN:8085,fork TCP-CONNECT:127.0.0.1:7101 &