[MusicStore] Move to GenericHost (#24284)

This commit is contained in:
Kahbazi 2020-07-24 23:51:13 +04:30 committed by GitHub
parent 3ea1fc7aac
commit 990e639c08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 46 deletions

View File

@ -1,72 +1,78 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.HttpSys; using Microsoft.AspNetCore.Server.HttpSys;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace MusicStore namespace MusicStore
{ {
public static class Program public static class Program
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var config = new ConfigurationBuilder() var config = new ConfigurationBuilder()
.AddCommandLine(args) .AddCommandLine(args)
.AddEnvironmentVariables(prefix: "ASPNETCORE_") .AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build(); .Build();
var builder = new WebHostBuilder() var builder = new HostBuilder()
.UseConfiguration(config) .ConfigureWebHost(webHostBuilder =>
.UseIISIntegration() {
.UseStartup("MusicStore") webHostBuilder
.UseConfiguration(config)
.UseIISIntegration()
.UseStartup("MusicStore");
var environment = webHostBuilder.GetSetting("environment") ??
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.Equals(webHostBuilder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal))
{
if (string.Equals(environment, "NtlmAuthentication", System.StringComparison.Ordinal))
{
// Set up NTLM authentication for WebListener like below.
// For IIS and IISExpress: Use inetmgr to setup NTLM authentication on the application vDir or
// modify the applicationHost.config to enable NTLM.
webHostBuilder.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.NTLM;
options.Authentication.AllowAnonymous = false;
});
}
else
{
webHostBuilder.UseHttpSys();
}
}
else
{
webHostBuilder.UseKestrel();
}
// In Proc
webHostBuilder.UseIIS();
webHostBuilder.ConfigureLogging(factory =>
{
factory.AddConsole();
var logLevel = string.Equals(environment, "Development", StringComparison.Ordinal) ? LogLevel.Information : LogLevel.Warning;
factory.SetMinimumLevel(logLevel);
// Turn off Info logging for EF commands
factory.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
});
})
.UseDefaultServiceProvider((context, options) => { .UseDefaultServiceProvider((context, options) => {
options.ValidateScopes = true; options.ValidateScopes = true;
}); });
var environment = builder.GetSetting("environment") ??
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.Equals(builder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal))
{
if (string.Equals(environment, "NtlmAuthentication", System.StringComparison.Ordinal))
{
// Set up NTLM authentication for WebListener like below.
// For IIS and IISExpress: Use inetmgr to setup NTLM authentication on the application vDir or
// modify the applicationHost.config to enable NTLM.
builder.UseHttpSys(options =>
{
options.Authentication.Schemes = AuthenticationSchemes.NTLM;
options.Authentication.AllowAnonymous = false;
});
}
else
{
builder.UseHttpSys();
}
}
else
{
builder.UseKestrel();
}
// In Proc
builder.UseIIS();
builder.ConfigureLogging(factory =>
{
factory.AddConsole();
var logLevel = string.Equals(environment, "Development", StringComparison.Ordinal) ? LogLevel.Information : LogLevel.Warning;
factory.SetMinimumLevel(logLevel);
// Turn off Info logging for EF commands
factory.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
});
var host = builder.Build(); var host = builder.Build();
host.Run(); return host.RunAsync();
} }
} }
} }