「ASP.NETコアパフォーマンスシリーズ」ASP.NETコアの起動手順(1)

オリジナル: スタートアップ「ASP.NETコアパフォーマンスシリーズ」ASP.NETコア(1)

まず、最初からすべて

説明:ゼロから始めるべきである真実を知っている、のコードファーストをしましょう

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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を検討する必要があるが、実際の状況を深く関わる。簡素化の問題の多くは、この記事を説明するために

第二に、私たちはいくつかのデータ構造に精通してみましょう

2.1 IHostBuilder(一時的に追加するには、このフォローアップを書きます)

コードをコピー
パブリック インターフェイスIHostBuilder
{
    IDictionaryを < オブジェクトオブジェクト > プロパティ
    {
        取得;
    }
  // 設定のホストプログラム 
    IHostBuilder ConfigureHostConfiguration(アクション<IConfigurationBuilder> configureDelegate);
  // 設定アプリケーション 
    IHostBuilder ConfigureAppConfiguration(アクション<HostBuilderContext、IConfigurationBuilder> configureDelegate);
  // サービスコンテナに追加 
    IHostBuilder ConfigureServices(アクション<HostBuilderContext、IServiceCollection> configureDelegate );
    UseServiceProviderFactory IHostBuilder <TContainerBuilder>(IServiceProviderFactory <TContainerBuilder> 工場);
  // のServiceProviderを作成するための工場をカバーします。
    UseServiceProviderFactory IHostBuilder <TContainerBuilder>(機能<HostBuilderContext、IServiceProviderFactory <TContainerBuilder >> 工場);
 / / コンフィギュレーション依存性注入容器 
    IHostBuilder ConfigureContainer <TContainerBuilder>(アクション<HostBuilderContext、TContainerBuilder> configureDelegate)。

    IHostビルド();
}
コードをコピー

簡単な分析と憶測を描く:CreateHostBuilderはAsp.netコアを設定し、元の材料の多くを必要と:(など、環境を含む)の構成情報、材料のすべての種類は、IOCのために必要な、必要なWebホストプログラムを作成するには、その後、上記の材料をホスティング同時に、ウェブホストを渡す.......

 

第三に、何をすべきかを内部CreateDefaultBuilderの道

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public static IHostBuilder CreateDefaultBuilder( string [] args)
{
     HostBuilder hostBuilder = new HostBuilder();
     hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());      //设置Host的配置信息
     hostBuilder.ConfigureHostConfiguration((Action<IConfigurationBuilder>) delegate (IConfigurationBuilder config)
     {
         config.AddEnvironmentVariables( "DOTNET_" );
         if (args != null )
         {
             config.AddCommandLine(args);
         }
     });      //设置应用程序的配置信息
     hostBuilder.ConfigureAppConfiguration((Action<HostBuilderContext, IConfigurationBuilder>) delegate (HostBuilderContext hostingContext, IConfigurationBuilder config)
     {
         IHostEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
         config.AddJsonFile( "appsettings.json" , optional: true , reloadOnChange: true ).AddJsonFile( "appsettings." + hostingEnvironment.EnvironmentName + ".json" , optional: true , reloadOnChange: true );
         if (hostingEnvironment.IsDevelopment() && ! string .IsNullOrEmpty(hostingEnvironment.ApplicationName))
         {
             Assembly assembly = Assembly.Load( new AssemblyName(hostingEnvironment.ApplicationName));
             if (assembly != null )
             {
                 config.AddUserSecrets(assembly, optional: true );
             }
         }
         config.AddEnvironmentVariables();
         if (args != null )
         {
             config.AddCommandLine(args);
         }
     }) //设置日志的相关配置       .ConfigureLogging((Action<HostBuilderContext, ILoggingBuilder>)delegate(HostBuilderContext hostingContext, ILoggingBuilder logging)
     {
         bool num = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
         if (num)
         {
             logging.AddFilter<EventLogLoggerProvider>((LogLevel level) => level >= LogLevel.Warning);
         }
         logging.AddConfiguration(hostingContext.Configuration.GetSection( "Logging" ));
         logging.AddConsole();
         logging.AddDebug();
         logging.AddEventSourceLogger();
         if (num)
         {
             logging.AddEventLog();
         }
     }).UseDefaultServiceProvider((Action<HostBuilderContext, ServiceProviderOptions>) delegate (HostBuilderContext context, ServiceProviderOptions options)
     {
         bool validateOnBuild = options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
         options.ValidateOnBuild = validateOnBuild;
     });
     return hostBuilder;
}

我们先总结一些知识点和注意事项

1.程序的默认的根目录采用了Directory.GetCurrentDirectory()也就是Environment.CurrentDirectory,那么这意味着我们在用其它程序启动应用时需要手动指定当前工作目录,避免发生变化.

2.asp.net core中两个路径的区别

?
1
2
ContentRoot:  C:\MyApp
WebRoot:      C:\MyApp\wwwroot

3.asp.net core中配置文件的优先级问题

命令行>环境变量>自我订制的配置(AddUserSecrets)>和当前环境相匹配的appsettings.json中的配置>大于appsettings.json中的配置

  关于AddUserSecrets是什么这里简单一言以蔽之:每个开发人员有自己特性的配置数据,这些配置信息仅仅属于个人,不能提交给团队成员,

  但是又不想不团队共有的配置所影响. 剩下的自行去了解,关键是上面的优先级

  和当前环境相匹配的appsettings.json如appsettings.Development.json

4.系统会默认添加日志行为如下

  a.windows会添加事件(level >= LogLevel.Warning)

  b.默认会将采用appsettings.json中Logging节点下的配置情况

  c.日志信息默认会显示咋控制台和调试中

 

四、 Startup 类

主要作用:配置服务和应用的请求管道。ASP.NET Core 按照约定命名为 Startup作为 Startup 类:

1.可选择性地包括 ConfigureServices 方法以配置应用的服务 。 服务是一个提供应用功能的可重用组件。

  在 ConfigureServices 中注册服务,并通过依赖关系注入 (DI) 或 ApplicationServices 在整个应用中使用服务 。


2.包括 Configure 方法以创建应用的请求处理管道。


在启动时,ASP.NET Core会调用 ConfigureServices 和 Configure:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Startup
{
     public Startup(IConfiguration configuration)
     {
         Configuration = configuration;
     }
     public IConfiguration Configuration { get ; }
     // 该方法会被运行时自动调用,通过此方法可将service添加到容器中.
     public void ConfigureServices(IServiceCollection services)
     {
         services.AddControllersWithViews();
     }
     // 该方法会被运行时自动调用. 使用此方法配置 HTTP request pipeline.
     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
         if (env.IsDevelopment())
         {
             app.UseDeveloperExceptionPage();
         }
         else
         {
             app.UseExceptionHandler( "/Home/Error" );
             // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
             app.UseHsts();
         }
         app.UseHttpsRedirection();
         app.UseStaticFiles();
         app.UseRouting();
         app.UseAuthorization();
         app.UseEndpoints(endpoints =>
         {
             endpoints.MapControllerRoute(
                 name: "default" ,
                 pattern: "{controller=Home}/{action=Index}/{id?}" );
         });
     }
}

 

スタートアップ概要:

  プログラムがホストに割り当てられたwebBuilder.UseStartup <スタートアップ>()スタートアップで起動したとき私たちは、最初の部分から見ることができます

注:ホストは、スタートアップクラスのコンストラクタが使用可能な特定のサービスを提供することができます。アプリケーション自体は、あなたがConfigureServices法で必要な他のサービスを追加することができます。

提供これらのアプリケーションやホストサービスを設定して、アプリケーション全体で使用することができます。

:一般的なホスト(IHostBuilder)を使用する場合、唯一の以下のサービスは、スタートアップの種類のコンストラクタに注入されます
  IWebHostEnvironment
  IHostEnvironment
  IConfiguration

 

  継続する、してください正しい欠陥が歓迎します。

 

おすすめ

転載: www.cnblogs.com/lonelyxmas/p/12292921.html