重新整理 .net core 实践篇————配置应用[一]

本来想整理到<<重新整理.net core 计1400篇>>里面去,但是后来一想,整理 .net core 实践篇 是偏于实践,故而分开。

因为是重新整理,那么就从配置开始整理。以下只是个人理解,如有错误,望请指点谢谢。

正文

在我们创建好一个应用的时候,那么出现在我们视野的是一个这样的东西:

public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }

看到这种:

CreateHostBuilder(args).Build().Run();

显然是建设者模式。

那么前面的基本就是在做一个构建。

既然是一个建设者模式,那么就来看一下这个的构建器是什么?

重新整理 .net core 实践篇————配置应用[一]

是一个叫做IHOSTBUILDER 的东西哈,而显然从命名上看这是一个接口哈。那么看下这个接口是啥:

重新整理 .net core 实践篇————配置应用[一]

跟配置相关得到也就是那几个Configure开头的那4个东西。

后面两个UseServiceProviderFactory后面系列再说。

// // 摘要: // Sets up the configuration for the remainder of the build process and application. // This can be called multiple times and the results will be additive. The results // will be available at Microsoft.Extensions.Hosting.HostBuilderContext.Configuration // for subsequent operations, as well as in Microsoft.Extensions.Hosting.IHost.Services. // // 参数: // configureDelegate: // The delegate for configuring the Microsoft.Extensions.Configuration.IConfigurationBuilder // that will be used to construct the Microsoft.Extensions.Configuration.IConfiguration // for the application. // // 返回结果: // The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining. IHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate);

这个函数的大致意思是说构件一个IConfigurationBuilder,这个the remainder of the build process and application.的配置。

// 摘要: // Enables configuring the instantiated dependency container. This can be called // multiple times and the results will be additive. // // 参数: // configureDelegate: // The delegate which configures the builder. // // 类型参数: // TContainerBuilder: // The type of builder. // // 返回结果: // The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining. IHostBuilder ConfigureContainer<TContainerBuilder>(Action<HostBuilderContext, TContainerBuilder> configureDelegate);

依赖容器相关的哈。

ConfigureHostConfiguration 和 ConfigureServices 这两个就不贴了,分别是一些坏境配置和服务配置。

那么启动一下,看下他们的执行顺序是否和我们代码的书写顺序是否相同,也就是说不管我如何调换顺序他们的执行都是按照某种规则。

那么这里加上log,通过log的方式来查看执行顺序,如下:

public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((builder) => { Console.WriteLine("ConfigureAppConfiguration"); }).ConfigureServices(builder => { Console.WriteLine("ConfigureServices"); }).ConfigureHostConfiguration(builder => { Console.WriteLine("ConfigureHostConfiguration"); }) .ConfigureWebHostDefaults(webBuilder => { Console.WriteLine("ConfigureWebHostDefaults"); webBuilder.UseStartup<Startup>(); }); }

startup.cs

public class Startup { public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Console.WriteLine("Startup"); Configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { Console.WriteLine("Startup.ConfigureServices"); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { Console.WriteLine("Startup.Configure"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapGet("http://www.likecs.com/", async context => { await context.Response.WriteAsync("Hello World!"); }); }); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zypffp.html