[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,40 +1,42 @@
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()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config) .UseConfiguration(config)
.UseIISIntegration() .UseIISIntegration()
.UseStartup("MusicStore") .UseStartup("MusicStore");
.UseDefaultServiceProvider((context, options) => {
options.ValidateScopes = true;
});
var environment = builder.GetSetting("environment") ?? var environment = webHostBuilder.GetSetting("environment") ??
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
if (string.Equals(builder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal)) if (string.Equals(webHostBuilder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal))
{ {
if (string.Equals(environment, "NtlmAuthentication", System.StringComparison.Ordinal)) if (string.Equals(environment, "NtlmAuthentication", System.StringComparison.Ordinal))
{ {
// Set up NTLM authentication for WebListener like below. // Set up NTLM authentication for WebListener like below.
// For IIS and IISExpress: Use inetmgr to setup NTLM authentication on the application vDir or // For IIS and IISExpress: Use inetmgr to setup NTLM authentication on the application vDir or
// modify the applicationHost.config to enable NTLM. // modify the applicationHost.config to enable NTLM.
builder.UseHttpSys(options => webHostBuilder.UseHttpSys(options =>
{ {
options.Authentication.Schemes = AuthenticationSchemes.NTLM; options.Authentication.Schemes = AuthenticationSchemes.NTLM;
options.Authentication.AllowAnonymous = false; options.Authentication.AllowAnonymous = false;
@ -42,18 +44,18 @@ namespace MusicStore
} }
else else
{ {
builder.UseHttpSys(); webHostBuilder.UseHttpSys();
} }
} }
else else
{ {
builder.UseKestrel(); webHostBuilder.UseKestrel();
} }
// In Proc // In Proc
builder.UseIIS(); webHostBuilder.UseIIS();
builder.ConfigureLogging(factory => webHostBuilder.ConfigureLogging(factory =>
{ {
factory.AddConsole(); factory.AddConsole();
@ -63,10 +65,14 @@ namespace MusicStore
// Turn off Info logging for EF commands // Turn off Info logging for EF commands
factory.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning); factory.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);
}); });
})
.UseDefaultServiceProvider((context, options) => {
options.ValidateScopes = true;
});
var host = builder.Build(); var host = builder.Build();
host.Run(); return host.RunAsync();
} }
} }
} }