// 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.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
{
///
/// Specify Kestrel as the server to be used by the web host.
///
///
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure.
///
///
/// The Microsoft.AspNetCore.Hosting.IWebHostBuilder.
///
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(services =>
{
// Don't override an already-configured transport
services.TryAddSingleton();
services.AddTransient, KestrelServerOptionsSetup>();
services.AddSingleton();
});
}
///
/// 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 options)
{
return hostBuilder.UseKestrel().ConfigureServices(services =>
{
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);
});
});
}
}
}