From a75b71a4499844e2c6e0da3b62e92b11b9221ebe Mon Sep 17 00:00:00 2001 From: "Chris Ross (ASP.NET)" Date: Tue, 21 Nov 2017 10:58:40 -0800 Subject: [PATCH] Expose WebHostBuilderContext in UseKestrel #1334 --- samples/Http2SampleApp/Program.cs | 13 +++------- samples/SampleApp/Startup.cs | 13 +++------- .../WebHostBuilderKestrelExtensions.cs | 26 +++++++++++++++++++ test/SystemdActivation/docker-entrypoint.sh | 4 +-- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/samples/Http2SampleApp/Program.cs b/samples/Http2SampleApp/Program.cs index 249c41347..5ee9cbda2 100644 --- a/samples/Http2SampleApp/Program.cs +++ b/samples/Http2SampleApp/Program.cs @@ -13,15 +13,6 @@ public class Program { 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 @@ public static void Main(string[] args) factory.SetMinimumLevel(LogLevel.Trace); factory.AddConsole(); }) - .UseKestrel(options => + .UseKestrel((context, options) => { + var basePort = context.Configuration.GetValue("BASE_PORT") ?? 5000; + // Run callbacks on the transport thread options.ApplicationSchedulingMode = SchedulingMode.Inline; diff --git a/samples/SampleApp/Startup.cs b/samples/SampleApp/Startup.cs index bf8ce0e1a..e0e471b63 100644 --- a/samples/SampleApp/Startup.cs +++ b/samples/SampleApp/Startup.cs @@ -43,22 +43,15 @@ public static Task Main(string[] args) 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("BASE_PORT") ?? 5000; + // Run callbacks on the transport thread options.ApplicationSchedulingMode = SchedulingMode.Inline; diff --git a/src/Kestrel/WebHostBuilderKestrelExtensions.cs b/src/Kestrel/WebHostBuilderKestrelExtensions.cs index f65a66ac4..fdc149e2f 100644 --- a/src/Kestrel/WebHostBuilderKestrelExtensions.cs +++ b/src/Kestrel/WebHostBuilderKestrelExtensions.cs @@ -57,5 +57,31 @@ public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Actio services.Configure(options); }); } + + /// + /// Specify Kestrel as the server to be used by the web host. + /// + /// + /// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure. + /// + /// A callback to configure Kestrel options. + /// + /// The Microsoft.AspNetCore.Hosting.IWebHostBuilder. + /// + public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Action configureOptions) + { + if (configureOptions == null) + { + throw new ArgumentNullException(nameof(configureOptions)); + } + + return hostBuilder.UseKestrel().ConfigureServices((context, services) => + { + services.Configure(options => + { + configureOptions(context, options); + }); + }); + } } } diff --git a/test/SystemdActivation/docker-entrypoint.sh b/test/SystemdActivation/docker-entrypoint.sh index c85c5fe0b..2b501ef6a 100644 --- a/test/SystemdActivation/docker-entrypoint.sh +++ b/test/SystemdActivation/docker-entrypoint.sh @@ -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 &