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

Commit

Permalink
Expose WebHostBuilderContext in UseKestrel #1334
Browse files Browse the repository at this point in the history
  • Loading branch information
Tratcher committed Nov 21, 2017
1 parent 6a793c2 commit a75b71a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
13 changes: 3 additions & 10 deletions samples/Http2SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,17 @@ 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) =>
{
// Set logging to the MAX.
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;

Expand Down
13 changes: 3 additions & 10 deletions samples/SampleApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int?>("BASE_PORT") ?? 5000;

// Run callbacks on the transport thread
options.ApplicationSchedulingMode = SchedulingMode.Inline;

Expand Down
26 changes: 26 additions & 0 deletions src/Kestrel/WebHostBuilderKestrelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,31 @@ public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Actio
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);
});
});
}
}
}
4 changes: 2 additions & 2 deletions test/SystemdActivation/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down

0 comments on commit a75b71a

Please sign in to comment.