一、和AspNetCore5相比,从6开始,将Program和Startup类合并,直接在入口类中启动服务和中间件。同时,项目可以启动miniApi,直接在Program中设置路由和控制器。实际项目中,还是推荐使用控制器的方式。
二、Furion定义了静态类Serve,对AspNetCore的启动类进行了封装,同时支持Startup的合并和分离写法,更加简单和易用。
三、应用启动时,框架为我们启动了内置的跨平台服务器Kestrel。Kestrel可以作为服务器自托管,也可以托管在IIS中(限Windows平台),或者在客户端与Kestrel之间架设反向代理服务器(跨平台,推荐)。Kestrel与IIS、Nginx的关系如下:
四、延伸知识点
1、AspNetCore6中实现Startup的方法之一,保持使用6版本的新类WebApplication
//Program.cs public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); var startup = new Startup(builder.Configuration); startup.ConfigureServices(builder.Services); var app = builder.Build(); startup.Configure(app, app.Environment); app.Run(); } } //Startup.cs public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; } public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); ...... } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (!env.IsDevelopment()) { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); ...... } }
2、Serve静态类源码,定义的Run方法的多个重载。可以看到,Furion使用的还是AspNetCore5及之前版本的Host静态类及其方法,这样用利于使用Startup。
public static class Serve { //默认 5000/5001 端口 public static void Run(string url = default) { Host.CreateDefaultBuilder(Environment.GetCommandLineArgs()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder.Inject() .ConfigureServices((context, services) => { // 默认配置控制器、动态 WebAPI 和规范化结果 services.AddCorsAccessor(); services.AddControllers().AddInjectWithUnifyResult(); }) .Configure((context, app) => { if (context.HostingEnvironment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseUnifyResultStatusCodes(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseCorsAccessor(); app.UseAuthentication(); app.UseAuthorization(); app.UseInject(string.Empty); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }); if (!string.IsNullOrWhiteSpace(url)) { webHostBuilder.UseUrls(url); } }) .Build() .Run(); } //Web 主机更多配置 //默认 5000/5001 端口 //泛型主机更多配置 public static void Run(Action<IWebHostBuilder> webHostBuilderConfigure, string url = default, Action<IHostBuilder> hostBuilderConfigure = default) { var hostBuilder = Host.CreateDefaultBuilder(Environment.GetCommandLineArgs()) .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder.Inject(); // 自定义端口 if (!string.IsNullOrWhiteSpace(url)) { webHostBuilder.UseUrls(url); } webHostBuilderConfigure(webHostBuilder); }); hostBuilderConfigure?.Invoke(hostBuilder); hostBuilder.Build().Run(); } }
标签:webHostBuilder,Run,string,AspNetCore,Startup,public,Furion,图解,app From: https://www.cnblogs.com/functionMC/p/16632531.html