Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit a75b71a

Browse files
committed
Expose WebHostBuilderContext in UseKestrel #1334
1 parent 6a793c2 commit a75b71a

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

samples/Http2SampleApp/Program.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,17 @@ public class Program
1313
{
1414
public static void Main(string[] args)
1515
{
16-
var configuration = new ConfigurationBuilder()
17-
.AddEnvironmentVariables()
18-
.Build();
19-
20-
if (!ushort.TryParse(configuration["BASE_PORT"], NumberStyles.None, CultureInfo.InvariantCulture, out var basePort))
21-
{
22-
basePort = 5000;
23-
}
24-
2516
var hostBuilder = new WebHostBuilder()
2617
.ConfigureLogging((_, factory) =>
2718
{
2819
// Set logging to the MAX.
2920
factory.SetMinimumLevel(LogLevel.Trace);
3021
factory.AddConsole();
3122
})
32-
.UseKestrel(options =>
23+
.UseKestrel((context, options) =>
3324
{
25+
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
26+
3427
// Run callbacks on the transport thread
3528
options.ApplicationSchedulingMode = SchedulingMode.Inline;
3629

samples/SampleApp/Startup.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,15 @@ public static Task Main(string[] args)
4343
Console.WriteLine("Unobserved exception: {0}", e.Exception);
4444
};
4545

46-
var configuration = new ConfigurationBuilder()
47-
.AddEnvironmentVariables()
48-
.Build();
49-
50-
if (!ushort.TryParse(configuration["BASE_PORT"], NumberStyles.None, CultureInfo.InvariantCulture, out var basePort))
51-
{
52-
basePort = 5000;
53-
}
54-
5546
var hostBuilder = new WebHostBuilder()
5647
.ConfigureLogging((_, factory) =>
5748
{
5849
factory.AddConsole();
5950
})
60-
.UseKestrel(options =>
51+
.UseKestrel((context, options) =>
6152
{
53+
var basePort = context.Configuration.GetValue<int?>("BASE_PORT") ?? 5000;
54+
6255
// Run callbacks on the transport thread
6356
options.ApplicationSchedulingMode = SchedulingMode.Inline;
6457

src/Kestrel/WebHostBuilderKestrelExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,31 @@ public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Actio
5757
services.Configure(options);
5858
});
5959
}
60+
61+
/// <summary>
62+
/// Specify Kestrel as the server to be used by the web host.
63+
/// </summary>
64+
/// <param name="hostBuilder">
65+
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
66+
/// </param>
67+
/// <param name="configureOptions">A callback to configure Kestrel options.</param>
68+
/// <returns>
69+
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder.
70+
/// </returns>
71+
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, KestrelServerOptions> configureOptions)
72+
{
73+
if (configureOptions == null)
74+
{
75+
throw new ArgumentNullException(nameof(configureOptions));
76+
}
77+
78+
return hostBuilder.UseKestrel().ConfigureServices((context, services) =>
79+
{
80+
services.Configure<KestrelServerOptions>(options =>
81+
{
82+
configureOptions(context, options);
83+
});
84+
});
85+
}
6086
}
6187
}

test/SystemdActivation/docker-entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
set -e
44

55
cd /publish
6-
systemd-socket-activate -l 8080 -E BASE_PORT=7000 dotnet SampleApp.dll &
6+
systemd-socket-activate -l 8080 -E ASPNETCORE_BASE_PORT=7000 dotnet SampleApp.dll &
77
socat TCP-LISTEN:8081,fork TCP-CONNECT:127.0.0.1:7000 &
88
socat TCP-LISTEN:8082,fork TCP-CONNECT:127.0.0.1:7001 &
9-
systemd-socket-activate -l /tmp/activate-kestrel.sock -E BASE_PORT=7100 dotnet SampleApp.dll &
9+
systemd-socket-activate -l /tmp/activate-kestrel.sock -E ASPNETCORE_BASE_PORT=7100 dotnet SampleApp.dll &
1010
socat TCP-LISTEN:8083,fork UNIX-CLIENT:/tmp/activate-kestrel.sock &
1111
socat TCP-LISTEN:8084,fork TCP-CONNECT:127.0.0.1:7100 &
1212
socat TCP-LISTEN:8085,fork TCP-CONNECT:127.0.0.1:7101 &

0 commit comments

Comments
 (0)