86 lines
3.3 KiB
C#
86 lines
3.3 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using Microsoft.AspNetCore.Hosting.Server;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions.Internal;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Microsoft.AspNetCore.Hosting
|
|
{
|
|
public static class WebHostBuilderKestrelExtensions
|
|
{
|
|
/// <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>
|
|
/// <returns>
|
|
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder.
|
|
/// </returns>
|
|
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder)
|
|
{
|
|
return hostBuilder.ConfigureServices(services =>
|
|
{
|
|
// Don't override an already-configured transport
|
|
services.TryAddSingleton<ITransportFactory, SocketTransportFactory>();
|
|
|
|
services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
|
|
services.AddSingleton<IServer, KestrelServer>();
|
|
});
|
|
}
|
|
|
|
/// <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="options">
|
|
/// A callback to configure Kestrel options.
|
|
/// </param>
|
|
/// <returns>
|
|
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder.
|
|
/// </returns>
|
|
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder, Action<KestrelServerOptions> options)
|
|
{
|
|
return hostBuilder.UseKestrel().ConfigureServices(services =>
|
|
{
|
|
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);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|