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

Commit 8b19fc9

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

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

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
}

0 commit comments

Comments
 (0)