From e2b38ff25c505fa6355537d445c0daed5e9f97d4 Mon Sep 17 00:00:00 2001 From: Kahbazi Date: Fri, 24 Jul 2020 20:20:26 +0430 Subject: [PATCH] [MusicStore] Move to GenericHost --- src/MusicStore/samples/MusicStore/Program.cs | 88 +++++++++++--------- 1 file changed, 47 insertions(+), 41 deletions(-) diff --git a/src/MusicStore/samples/MusicStore/Program.cs b/src/MusicStore/samples/MusicStore/Program.cs index ca81340f1f3a..6a5993d45910 100644 --- a/src/MusicStore/samples/MusicStore/Program.cs +++ b/src/MusicStore/samples/MusicStore/Program.cs @@ -1,72 +1,78 @@ using System; using System.IO; +using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.HttpSys; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace MusicStore { public static class Program { - public static void Main(string[] args) + public static Task Main(string[] args) { var config = new ConfigurationBuilder() .AddCommandLine(args) .AddEnvironmentVariables(prefix: "ASPNETCORE_") .Build(); - var builder = new WebHostBuilder() - .UseConfiguration(config) - .UseIISIntegration() - .UseStartup("MusicStore") - .UseDefaultServiceProvider((context, options) => { - options.ValidateScopes = true; - }); + var builder = new HostBuilder() + .ConfigureWebHost(webHostBuilder => + { + webHostBuilder + .UseConfiguration(config) + .UseIISIntegration() + .UseStartup("MusicStore"); - var environment = builder.GetSetting("environment") ?? + var environment = webHostBuilder.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 => + if (string.Equals(webHostBuilder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", System.StringComparison.Ordinal)) { - options.Authentication.Schemes = AuthenticationSchemes.NTLM; - options.Authentication.AllowAnonymous = false; - }); - } - else - { - builder.UseHttpSys(); - } - } - else - { - builder.UseKestrel(); - } + 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 - builder.UseIIS(); + // In Proc + webHostBuilder.UseIIS(); - builder.ConfigureLogging(factory => - { - factory.AddConsole(); + webHostBuilder.ConfigureLogging(factory => + { + factory.AddConsole(); - var logLevel = string.Equals(environment, "Development", StringComparison.Ordinal) ? LogLevel.Information : LogLevel.Warning; - factory.SetMinimumLevel(logLevel); + 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); - }); + // Turn off Info logging for EF commands + factory.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning); + }); + }) + .UseDefaultServiceProvider((context, options) => { + options.ValidateScopes = true; + }); var host = builder.Build(); - host.Run(); + return host.RunAsync(); } } }