From 3d0dfb064a3de64902e4c7f40dd784f3c2488d4d Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Mon, 23 Dec 2024 02:36:16 +0800 Subject: [PATCH] Remove Startup.cs --- .../src/CapSample.HttpApi.Host/Program.cs | 82 +++++++++---------- .../src/CapSample.HttpApi.Host/Startup.cs | 20 ----- 2 files changed, 41 insertions(+), 61 deletions(-) delete mode 100644 sample/CapSample/src/CapSample.HttpApi.Host/Startup.cs diff --git a/sample/CapSample/src/CapSample.HttpApi.Host/Program.cs b/sample/CapSample/src/CapSample.HttpApi.Host/Program.cs index a9452ac..1d1d5eb 100644 --- a/sample/CapSample/src/CapSample.HttpApi.Host/Program.cs +++ b/sample/CapSample/src/CapSample.HttpApi.Host/Program.cs @@ -1,56 +1,56 @@ using System; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; -namespace CapSample +namespace CapSample; + +public class Program { - public class Program + public async static Task Main(string[] args) { - public static int Main(string[] args) - { - Log.Logger = new LoggerConfiguration() + Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Debug() #else - .MinimumLevel.Information() + .MinimumLevel.Information() #endif - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) - .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) - .Enrich.FromLogContext() - .WriteTo.Async(c => c.File("Logs/logs.txt")) -#if DEBUG - .WriteTo.Async(c => c.Console()) -#endif - .CreateLogger(); + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) + .Enrich.FromLogContext() + .WriteTo.Async(c => c.File("Logs/logs.txt")) + .WriteTo.Async(c => c.Console()) + .CreateLogger(); - try - { - Log.Information("Starting CapSample.HttpApi.Host."); - CreateHostBuilder(args).Build().Run(); - return 0; - } - catch (Exception ex) - { - Log.Fatal(ex, "Host terminated unexpectedly!"); - return 1; - } - finally + try + { + Log.Information("Starting web host."); + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson() + .UseAutofac() + .UseSerilog(); + await builder.AddApplicationAsync(); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); + return 0; + } + catch (Exception ex) + { + if (ex is HostAbortedException) { - Log.CloseAndFlush(); + throw; } - } - internal static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .AddAppSettingsSecretsJson() - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }) - .UseAutofac() - .UseSerilog(); + Log.Fatal(ex, "Host terminated unexpectedly!"); + return 1; + } + finally + { + Log.CloseAndFlush(); + } } -} +} \ No newline at end of file diff --git a/sample/CapSample/src/CapSample.HttpApi.Host/Startup.cs b/sample/CapSample/src/CapSample.HttpApi.Host/Startup.cs deleted file mode 100644 index 745a0c0..0000000 --- a/sample/CapSample/src/CapSample.HttpApi.Host/Startup.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; - -namespace CapSample -{ - public class Startup - { - public void ConfigureServices(IServiceCollection services) - { - services.AddApplication(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - app.InitializeApplication(); - } - } -}