關(guān)于我們
書(shū)單推薦
新書(shū)推薦
|
ASP.NET Core 6框架揭秘(上下冊(cè)) 讀者對(duì)象:對(duì)ASP.NET MVC編程模式具有基本了解的.NET開(kāi)發(fā)人員。
本書(shū)主要介紹ASP.NET Core框架最核心的部分,即由一個(gè)服務(wù)器和若干中間件構(gòu)建的管道。本書(shū)共分為5篇:"第1篇 初識(shí)編程(第1章)”列舉一系列極簡(jiǎn)的實(shí)例為讀者提供基本的編程體驗(yàn),"第2 篇 基礎(chǔ)框架(第2~13章)”主要介紹了一系列支撐ASP.NET Core的基礎(chǔ)框架,"第3篇 承載系統(tǒng)(第14~17章)”主要介紹了ASP.NET Core應(yīng)用的承載流程,"第4篇 服務(wù)器概述(第18章)”列舉一系列常見(jiàn)的服務(wù)器類(lèi)型并對(duì)它們進(jìn)行了比較,"第5 篇 中間件(第19~30章)”系統(tǒng)地介紹了一系列預(yù)定義的中間件。
蔣金楠,網(wǎng)名Artech,高級(jí)軟件顧問(wèn)。微軟6任MVP(Solutions Architecture、Connected System與Microsoft Integration)。中國(guó) .NET 技術(shù)領(lǐng)域資深專(zhuān)家,著有《WCF全面解析》)、《ASP.NET MVC 5框架揭秘》等多部領(lǐng)域暢銷(xiāo)著作。對(duì).NET Framework、C#、ASP.NET、SQL Server、設(shè)計(jì)模式、軟件架構(gòu),以及主流開(kāi)源框架有著深入研究,同時(shí)對(duì).NET Remoting、MSMQ通信技術(shù)有深入的理解。博客園頭牌博主,51CTO十佳博客,知名技術(shù)公眾號(hào)“大內(nèi)老A”號(hào)主。
第 1 篇 初識(shí)編程
第 1 章 編程體驗(yàn) ........................................................................................................................... 2 1.1 控制臺(tái)程序 ........................................................................................................................ 2 1.1.1 構(gòu)建開(kāi)發(fā)環(huán)境 ........................................................................................................ 2 1.1.2 命令行構(gòu)建 .NET 應(yīng)用 ......................................................................................... 3 1.2 ASP.NET 應(yīng)用 .................................................................................................................... 8 1.2.1 launchSettings.json ................................................................................................. 8 1.2.2 Minimal API ......................................................................................................... 11 1.2.3 中間件 ................................................................................................................. 13 1.2.4 配置選項(xiàng) .............................................................................................................. 17 1.2.5 診斷日志 .............................................................................................................. 19 1.2.6 路由 ..................................................................................................................... 20 1.3 MVC ................................................................................................................................ 21 1.3.1 定義 Controller ..................................................................................................... 21 1.3.2 引入視圖 .............................................................................................................. 22 1.4 gRPC ................................................................................................................................ 24 1.4.1 定義服務(wù) .............................................................................................................. 24 1.4.2 實(shí)現(xiàn)和承載 .......................................................................................................... 26 1.4.3 調(diào)用服務(wù) .............................................................................................................. 28 1.5 Dapr.................................................................................................................................. 29 1.5.1 構(gòu)建開(kāi)發(fā)環(huán)境 ...................................................................................................... 29 1.5.2 服務(wù)調(diào)用 .............................................................................................................. 30 1.5.3 狀態(tài)管理 .............................................................................................................. 34 1.5.4 發(fā)布訂閱 .............................................................................................................. 38 1.5.5 Actor 模型 ............................................................................................................ 40 第 2 篇 基礎(chǔ)框架 第 2 章 依賴(lài)注入(上) ............................................................................................................. 44 2.1 控制反轉(zhuǎn) .......................................................................................................................... 44 2.1.1 流程控制的反轉(zhuǎn) .................................................................................................. 44 2.1.2 好萊塢法則 .......................................................................................................... 47 2.1.3 流程定制 .............................................................................................................. 47 2.2 IoC 模式 ........................................................................................................................... 48 2.2.1 模板方法 .............................................................................................................. 48 2.2.2 工廠(chǎng)方法 .............................................................................................................. 49 2.2.3 抽象工廠(chǎng) .............................................................................................................. 51 2.3 依賴(lài)注入 .......................................................................................................................... 53 2.3.1 由容器提供對(duì)象 .................................................................................................. 53 2.3.2 3 種注入方式 ....................................................................................................... 55 2.3.3 Service Locator 模式 ............................................................................................ 57 2.4 一個(gè)簡(jiǎn)易版的依賴(lài)注入容器 ............................................................................................ 59 2.4.1 編程體驗(yàn) .............................................................................................................. 59 2.4.2 設(shè)計(jì)與實(shí)現(xiàn) .......................................................................................................... 63 2.4.3 擴(kuò)展方法 .............................................................................................................. 69 第 3 章 依賴(lài)注入(下) ............................................................................................................. 73 3.1 利用容器提供服務(wù) ........................................................................................................... 73 3.1.1 服務(wù)的注冊(cè)與消費(fèi) ............................................................................................... 73 3.1.2 生命周期 .............................................................................................................. 76 3.1.3 服務(wù)注冊(cè)的驗(yàn)證 .................................................................................................. 79 3.2 服務(wù)注冊(cè) .......................................................................................................................... 82 3.2.1 ServiceDescriptor .................................................................................................. 82 3.2.2 IServiceCollection ................................................................................................. 83 3.3 服務(wù)的消費(fèi) ...................................................................................................................... 87 3.3.1 IServiceProvider ................................................................................................... 87 3.3.2 服務(wù)實(shí)例的創(chuàng)建 .................................................................................................. 89 3.3.3 生命周期 .............................................................................................................. 91 3.3.4 ActivatorUtilities .................................................................................................. 96 3.4 擴(kuò)展................................................................................................................................ 101 3.4.1 適配 ................................................................................................................... 101 3.4.2 IServiceProviderFactory<TContainerBuilder> ..................................................... 102 3.4.3 整合第三方依賴(lài)注入框架 ................................................................................. 102 第 4 章 文件系統(tǒng) ....................................................................................................................... 107 4.1 抽象的文件系統(tǒng) ............................................................................................................. 107 4.1.1 樹(shù)形層次結(jié)構(gòu) .................................................................................................... 107 4.1.2 讀取文件內(nèi)容 .................................................................................................... 109 4.1.3 監(jiān)控文件的變化 ................................................................................................ 111 4.2 文件與目錄 .................................................................................................................... 112 4.2.1 IChangeToken ..................................................................................................... 113 4.2.2 IFileProvider ....................................................................................................... 114 4.2.3 兩個(gè)特殊的文件系統(tǒng) ......................................................................................... 116 4.3 物理文件系統(tǒng) ................................................................................................................ 119 4.3.1 PhysicalFileInfo .................................................................................................. 119 4.3.2 PhysicalDirectoryInfo ......................................................................................... 120 4.3.3 PhysicalDirectoryContents .................................................................................. 120 4.3.4 NotFoundDirectoryContents ............................................................................... 121 4.3.5 PhysicalFilesWatcher .......................................................................................... 121 4.4 內(nèi)嵌文件系統(tǒng) ................................................................................................................ 123 4.4.1 將項(xiàng)目文件變成內(nèi)嵌資源 ................................................................................. 123 4.4.2 讀取資源文件 .................................................................................................... 124 4.4.3 EmbeddedFileProvider ........................................................................................ 125 第 5 章 配置選項(xiàng)(上) ........................................................................................................... 128 5.1 讀取配置信息 ................................................................................................................ 128 5.1.1 編程模型三要素 ................................................................................................ 128 5.1.2 以“鍵-值”對(duì)的形式讀取配置 ........................................................................ 129 5.1.3 讀取結(jié)構(gòu)化的配置 ............................................................................................ 130 5.1.4 將結(jié)構(gòu)化配置直接綁定為對(duì)象 .......................................................................... 133 5.1.5 將配置定義在文件中 ......................................................................................... 133 5.1.6 根據(jù)環(huán)境動(dòng)態(tài)加載配置文件 ............................................................................. 134 5.1.7 配置內(nèi)容的同步 ................................................................................................ 136 5.2 配置模型 ........................................................................................................................ 137 5.2.1 數(shù)據(jù)結(jié)構(gòu)及其轉(zhuǎn)換 ............................................................................................. 137 5.2.2 IConfiguration .................................................................................................... 138 5.2.3 IConfigurationProvider ....................................................................................... 140 5.2.4 IConfigurationSource .......................................................................................... 142 5.2.5 IConfigurationBuilder ......................................................................................... 143 5.2.6 ConfigurationManager ........................................................................................ 143 5.3 配置綁定 ........................................................................................................................ 144 5.3.1 綁定配置項(xiàng)的值 ................................................................................................ 145 5.3.2 綁定復(fù)合對(duì)象 .................................................................................................... 147 5.3.3 綁定集合 ............................................................................................................ 149 5.3.4 綁定字典 ............................................................................................................ 151 5.4 配置的同步 .................................................................................................................... 152 5.4.1 配置數(shù)據(jù)流 ........................................................................................................ 152 5.4.2 ConfigurationReloadToken .................................................................................. 153 5.4.3 ConfigurationRoot .............................................................................................. 153 5.4.4 ConfigurationSection .......................................................................................... 156 5.5 多樣性的配置源 ............................................................................................................. 157 5.5.1 MemoryConfigurationSource .............................................................................. 157 5.5.2 EnvironmentVariablesConfigurationSource ......................................................... 158 5.5.3 CommandLineConfigurationSource .................................................................... 160 5.5.4 FileConfigurationSource ..................................................................................... 163 5.5.5 StreamConfigurationSource ................................................................................ 173 5.5.6 ChainedConfigurationSource ............................................................................... 175 第 6 章 配置選項(xiàng)(下) ........................................................................................................... 177 6.1 Options 模式 ................................................................................................................... 177 6.1.1 將配置綁定為 Options 對(duì)象 ............................................................................... 177 6.1.2 提供具名的 Options ........................................................................................... 179 6.1.3 配置源的同步 .................................................................................................... 180 6.1.4 直接初始化 Options 對(duì)象 ................................................................................... 182 6.1.5 根據(jù)依賴(lài)服務(wù)的 Options 設(shè)置 ........................................................................... 184 6.1.6 驗(yàn)證 Options 的有效性 ...................................................................................... 186 6.2 Options 模型 ................................................................................................................... 187 6.2.1 OptionsManager<TOptions> ............................................................................... 187 6.2.2 IOptionsFactory<TOptions> ............................................................................... 189 6.2.3 IOptionsMonitorCache<TOptions> ..................................................................... 197 6.2.4 IOptionsMonitor<TOptions> ............................................................................... 198 6.3 依賴(lài)注入 ........................................................................................................................ 200 6.3.1 服務(wù)注冊(cè) ............................................................................................................ 200 6.3.2 IOptions<TOptions>與 IOptionsSnapshot<TOptions> ......................................... 206 6.3.3 集成配置系統(tǒng) .................................................................................................... 208 第 7 章 診斷日志(上) ........................................................................................................... 210 7.1 各種診斷日志形式 ......................................................................................................... 210 7.1.1 調(diào)試日志 ............................................................................................................ 210 7.1.2 跟蹤日志 ............................................................................................................ 211 7.1.3 事件日志 ............................................................................................................ 213 7.1.4 診斷日志 ............................................................................................................ 216 7.2 Debugger 調(diào)試日志 ........................................................................................................ 219 7.2.1 Debugger ............................................................................................................ 220 7.2.2 Debug ................................................................................................................. 221 7.3 TraceSource 跟蹤日志 .................................................................................................... 222 7.3.1 跟蹤日志模型三要素 ......................................................................................... 222 7.3.2 預(yù)定義 TraceListener .......................................................................................... 231 7.3.3 Trace .................................................................................................................. 235 7.4 EventSource 事件日志 .................................................................................................... 237 7.4.1 EventSource ....................................................................................................... 237 7.4.2 EventListener ...................................................................................................... 243 7.4.3 活動(dòng)跟蹤 ............................................................................................................ 247 7.5 DiagnosticSource 診斷日志 ............................................................................................ 251 7.5.1 標(biāo)準(zhǔn)的觀(guān)察者模式 ............................................................................................ 251 7.5.2 AnonymousObserver<T> .................................................................................... 253 7.5.3 強(qiáng)類(lèi)型的日志事件訂閱 ..................................................................................... 256 7.5.4 針對(duì)活動(dòng)的跟蹤 ................................................................................................ 258 第 8 章 診斷日志(中) ........................................................................................................... 260 8.1 統(tǒng)一日志編程模式 ......................................................................................................... 260 8.1.1 日志輸出 ............................................................................................................ 260 8.1.2 日志過(guò)濾 ............................................................................................................ 265 8.1.3 日志范圍 ............................................................................................................ 271 8.1.4 LoggerMessage ................................................................................................... 273 8.2 日志模型詳解 ................................................................................................................ 274 8.2.1 日志模型三要素 ................................................................................................ 274 8.2.2 ILogger ............................................................................................................... 275 8.2.3 ILoggerProvider .................................................................................................. 277 8.2.4 ILoggerFactory ................................................................................................... 277 8.2.5 LoggerMessage ................................................................................................... 281 8.3 日志范圍 ........................................................................................................................ 283 8.3.1 調(diào)用鏈跟蹤 ........................................................................................................ 283 8.3.2 服務(wù)范圍堆棧 .................................................................................................... 294 8.3.3 Activity 的應(yīng)用 .................................................................................................. 296 8.4 依賴(lài)注入 ........................................................................................................................ 297 8.4.1 核心服務(wù) ............................................................................................................ 298 8.4.2 配置 ................................................................................................................... 300 8.4.3 日志過(guò)濾規(guī)則 .................................................................................................... 303 第 9 章 診斷日志(下) ........................................................................................................... 306 9.1 控制臺(tái) ............................................................................................................................ 306 9.1.1 ConsoleFormatter ................................................................................................ 306 9.1.2 ConsoleLogger .................................................................................................... 313 9.1.3 ConsoleLoggerProvider ....................................................................................... 315 9.1.4 服務(wù)注冊(cè) ............................................................................................................ 316 9.2 調(diào)試器 ............................................................................................................................ 321 9.2.1 DebugLogger ...................................................................................................... 322 9.2.2 DebugLoggerProvider ......................................................................................... 323 9.3 TraceSource 日志 ............................................................................................................ 323 9.3.1 TraceSourceLogger ............................................................................................. 323 9.3.2 TraceSourceLoggerProvider ................................................................................ 325 9.4 EventSource 日志 ........................................................................................................... 327 9.4.1 LoggingEventSource ........................................................................................... 327 9.4.2 EventSourceLogger............................................................................................. 328 9.4.3 EventSourceLoggerProvider ............................................................................... 332 9.4.4 日志范圍 ............................................................................................................ 334 第 10 章 對(duì)象池 ......................................................................................................................... 338 10.1 利用對(duì)象池復(fù)用對(duì)象 ................................................................................................... 338 10.1.1 對(duì)象的“借”與“還” ................................................................................... 338 10.1.2 依賴(lài)注入 .......................................................................................................... 340 10.1.3 池化對(duì)象策略 .................................................................................................. 340 10.1.4 對(duì)象池的大小 .................................................................................................. 341 10.1.5 對(duì)象的釋放 ...................................................................................................... 344 10.2 池化對(duì)象管理 .............................................................................................................. 345 10.2.1 IPooledObjectPolicy<T> ................................................................................... 345 10.2.2 ObjectPool<T> ................................................................................................. 346 10.2.3 ObjectPoolProvider ........................................................................................... 351 10.3 擴(kuò)展應(yīng)用 ...................................................................................................................... 352 10.3.1 池化集合 .......................................................................................................... 352 10.3.2 池化 StringBuilder ............................................................................................ 354 10.3.3 ArrayPool<T> ................................................................................................... 356 10.3.4 MemoryPool<T> .............................................................................................. 357 第 11 章 緩存 ............................................................................................................................. 359 11.1 將數(shù)據(jù)緩存起來(lái) ........................................................................................................... 359 11.1.1 將數(shù)據(jù)緩存在內(nèi)存中 ....................................................................................... 359 11.1.2 將數(shù)據(jù)緩存在 Redis 中 .................................................................................... 360 11.1.3 將數(shù)據(jù)緩存在 SQL Server 中 ........................................................................... 363 11.2 本地內(nèi)存緩存 ............................................................................................................... 365 11.2.1 ICacheEntry ...................................................................................................... 365 11.2.2 MemoryCacheEntryOptions .............................................................................. 368 11.2.3 IMemoryCache ................................................................................................. 369 11.3 分布式緩存 .................................................................................................................. 378 11.3.1 IDistributedCache ............................................................................................. 378 11.3.2 Redis 緩存 ........................................................................................................ 379 11.3.3 SQL Server 緩存 ............................................................................................... 382 第 12 章 HTTP 調(diào)用 ................................................................................................................. 385 12.1 HttpClient 的工廠(chǎng) ......................................................................................................... 385 12.1.1 手動(dòng)創(chuàng)建 HttpClient ......................................................................................... 385 12.1.2 使用 IHttpClientFactory 工廠(chǎng) ........................................................................... 387 12.1.3 直接注入 HttpClient ......................................................................................... 387 12.1.4 定制 HttpClient................................................................................................. 388 12.1.5 強(qiáng)類(lèi)型客戶(hù)端 .................................................................................................. 389 12.1.6 失敗重試 .......................................................................................................... 390 12.2 HttpMessageHandler 管道 ............................................................................................. 391 12.2.1 HttpMessageHandler ......................................................................................... 391 12.2.2 DelegatingHandler ............................................................................................ 392 12.2.3 診斷日志 .......................................................................................................... 396 12.2.4 復(fù)用 HttpClientHandler .................................................................................... 398 12.3 HttpClient 的構(gòu)建 ......................................................................................................... 400 12.3.1 HttpMessageHandlerBuilder ............................................................................. 400 12.3.2 HttpClientFactoryOptions ................................................................................. 401 12.3.3 IHttpMessageHandlerBuilderFilter .................................................................... 407 12.3.4 IHttpClientFactory ............................................................................................ 408 12.4 依賴(lài)注入 ...................................................................................................................... 418 12.4.1 基礎(chǔ)服務(wù)注冊(cè) .................................................................................................. 418 12.4.2 定制 HttpClient................................................................................................. 420 12.4.3 強(qiáng)類(lèi)型客戶(hù)端 .................................................................................................. 424 第 13 章 數(shù)據(jù)保護(hù) ..................................................................................................................... 430 13.1 加密與哈希 .................................................................................................................. 430 13.1.1 數(shù)據(jù)加密與解密............................................................................................... 430 13.1.2 設(shè)置加密內(nèi)容的有效期 ................................................................................... 433 13.1.3 撤銷(xiāo)密鑰 .......................................................................................................... 434 13.1.4 “瞬時(shí)”加解密............................................................................................... 435 13.1.5 密碼哈希 .......................................................................................................... 437 13.2 加密模型 ...................................................................................................................... 438 13.2.1 IAuthenticatedEncryptor ................................................................................... 438 13.2.2 IKey ................................................................................................................. 439 13.2.3 IKeyRing .......................................................................................................... 440 13.2.4 IKeyRingProvider ............................................................................................. 442 13.2.5 IDataProtector ................................................................................................... 443 13.3 密鑰管理 ...................................................................................................................... 452 13.3.1 KeyManagementOptions ................................................................................... 452 13.3.2 Key ................................................................................................................... 457 13.3.3 IKeyManager .................................................................................................... 459 13.3.4 KeyRingProvider .............................................................................................. 465 13.4 依賴(lài)注入 ...................................................................................................................... 472 13.4.1 注冊(cè)基礎(chǔ)服務(wù) .................................................................................................. 472 13.4.2 密鑰管理配置 .................................................................................................. 477 13.4.3 擴(kuò)展配置 .......................................................................................................... 478 附錄 A ........................................................................................................................................... 482 第 3 篇 承載系統(tǒng) 第 14 章 服務(wù)承載 ..................................................................................................................... 488 14.1 服務(wù)承載 ...................................................................................................................... 488 14.1.1 性能指標(biāo)收集服務(wù) ........................................................................................... 488 14.1.2 依賴(lài)注入 .......................................................................................................... 490 14.1.3 配置選項(xiàng) .......................................................................................................... 493 14.1.4 承載環(huán)境 .......................................................................................................... 496 14.1.5 日志 ................................................................................................................. 498 14.2 服務(wù)承載模型 .............................................................................................................. 500 14.2.1 IHostedService .................................................................................................. 500 14.2.2 IHost ................................................................................................................ 501 14.2.3 IHostBuilder ..................................................................................................... 506 14.3 服務(wù)承載流程 .............................................................................................................. 511 14.3.1 服務(wù)宿主 .......................................................................................................... 512 14.3.2 服務(wù)承載設(shè)置 .................................................................................................. 516 14.3.3 創(chuàng)建宿主 .......................................................................................................... 521 14.3.4 靜態(tài)類(lèi)型 Host .................................................................................................. 527 第 15 章 應(yīng)用承載(上) ......................................................................................................... 531 15.1 管道式的請(qǐng)求處理 ....................................................................................................... 531 15.1.1 承載方式的變遷 .............................................................................................. 531 15.1.2 中間件.............................................................................................................. 536 15.1.3 定義強(qiáng)類(lèi)型中間件 ........................................................................................... 539 15.1.4 按照約定定義中間件 ....................................................................................... 540 15.2 依賴(lài)注入 ...................................................................................................................... 542 15.2.1 服務(wù)注冊(cè) .......................................................................................................... 542 15.2.2 服務(wù)注入 .......................................................................................................... 544 15.2.3 生命周期 .......................................................................................................... 546 15.3 配置 .............................................................................................................................. 551 15.3.1 初始化配置 ...................................................................................................... 551 15.3.2 以“鍵-值”對(duì)形式讀取和修改配置 ............................................................... 552 15.3.3 注冊(cè)配置源 ...................................................................................................... 553 15.4 承載環(huán)境 ...................................................................................................................... 554 15.4.1 IWebHostEnvironment ...................................................................................... 554 15.4.2 通過(guò)配置定制承載環(huán)境 ................................................................................... 556 15.4.3 設(shè)置監(jiān)聽(tīng)地址 .................................................................................................. 559 15.4.4 針對(duì)環(huán)境的編程............................................................................................... 560 第 16 章 應(yīng)用承載(中) ......................................................................................................... 564 16.1 中間件委托鏈 ............................................................................................................... 564 16.1.1 HttpContext ...................................................................................................... 564 16.1.2 中間件 .............................................................................................................. 565 16.1.3 中間件管道的構(gòu)建 ........................................................................................... 566 16.2 服務(wù)器 .......................................................................................................................... 567 16.2.1 IServer .............................................................................................................. 568 16.2.2 針對(duì)服務(wù)器的適配 ........................................................................................... 568 16.2.3 HttpListenerServer ............................................................................................ 570 16.3 承載服務(wù) ...................................................................................................................... 573 16.3.1 WebHostedService ............................................................................................ 573 16.3.2 WebHostBuilder ................................................................................................ 573 16.3.3 應(yīng)用構(gòu)建 .......................................................................................................... 575 第 17 章 應(yīng)用承載(下) ......................................................................................................... 577 17.1 共享上下文對(duì)象 ........................................................................................................... 577 17.1.1 HttpContext ...................................................................................................... 577 17.1.2 服務(wù)器適配 ...................................................................................................... 580 17.1.3 獲取上下文對(duì)象 .............................................................................................. 583 17.1.4 上下文對(duì)象的創(chuàng)建與釋放 ............................................................................... 584 17.1.5 RequestServices ................................................................................................ 585 17.2 IServer + IHttpApplication ............................................................................................ 587 17.2.1 IServer .............................................................................................................. 588 17.2.2 HostingApplication ........................................................................................... 588 17.2.3 診斷日志 .......................................................................................................... 591 17.3 中間件委托鏈 .............................................................................................................. 596 17.3.1 IApplicationBuilder .......................................................................................... 596 17.3.2 弱類(lèi)型中間件 .................................................................................................. 599 17.3.3 強(qiáng)類(lèi)型中間件 .................................................................................................. 602 17.3.4 注冊(cè)中間件 ...................................................................................................... 604 17.4 應(yīng)用的承載 .................................................................................................................. 604 17.4.1 GenericWebHostServiceOptions ........................................................................ 604 17.4.2 GenericWebHostService .................................................................................... 607 17.4.3 GenericWebHostBuilder .................................................................................... 609 17.4.4 ConfigureWebHostDefaults ............................................................................... 617 17.5 Minimal API ................................................................................................................. 619 17.5.1 WebApplication ................................................................................................ 620 17.5.2 WebApplication 的創(chuàng)建 .................................................................................... 622 17.5.3 工廠(chǎng)方法 .......................................................................................................... 631 第 4 篇 服務(wù)器概述 第 18 章 服務(wù)器 ......................................................................................................................... 634 18.1 自定義服務(wù)器 .............................................................................................................. 634 18.1.1 IServer .............................................................................................................. 634 18.1.2 請(qǐng)求和響應(yīng)特性 .............................................................................................. 637 18.1.3 StreamBodyFeature ........................................................................................... 638 18.1.4 HttpListenerServer ............................................................................................ 638 18.2 KestrelServer ................................................................................................................ 642 18.2.1 注冊(cè)終節(jié)點(diǎn) ...................................................................................................... 642 18.2.2 限制約束 .......................................................................................................... 652 18.2.3 其他設(shè)置 .......................................................................................................... 655 18.2.4 設(shè)計(jì)與實(shí)現(xiàn) ...................................................................................................... 655 18.3 HTTP.SYS .................................................................................................................... 668 18.3.1 HTTP.SYS 簡(jiǎn)介 ................................................................................................ 668 18.3.2 UseHttpSys ....................................................................................................... 669 18.3.3 HttpSysOptions ................................................................................................. 669 18.4 IIS ................................................................................................................................. 672 18.4.1 ASP.NET Core Module ..................................................................................... 672 18.4.2 In-Process 部署模式 ......................................................................................... 672 18.4.3 Out-of-Process 部署模式 .................................................................................. 675 18.4.4 <aspnetcore>配置 ............................................................................................. 678 第 5 篇 中間件 第 19 章 靜態(tài)文件 ..................................................................................................................... 682 19.1 搭建文件服務(wù)器 ........................................................................................................... 682 19.1.1 發(fā)布物理文件 .................................................................................................. 682 19.1.2 呈現(xiàn)目錄結(jié)構(gòu) .................................................................................................. 684 19.1.3 顯示默認(rèn)頁(yè)面 .................................................................................................. 685 19.1.4 映射媒體類(lèi)型 .................................................................................................. 688 19.2 處理文件請(qǐng)求 ............................................................................................................... 690 19.2.1 條件請(qǐng)求 .......................................................................................................... 690 19.2.2 區(qū)間請(qǐng)求 .......................................................................................................... 694 19.2.3 StaticFileMiddleware ........................................................................................ 696 19.3 處理目錄請(qǐng)求 ............................................................................................................... 700 19.3.1 DirectoryBrowserMiddleware ........................................................................... 700 19.3.2 DefaultFilesMiddleware .................................................................................... 704 第 20 章 路由 ............................................................................................................................. 708 20.1 路由映射 ...................................................................................................................... 708 20.1.1 注冊(cè)終節(jié)點(diǎn) ...................................................................................................... 708 20.1.2 設(shè)置內(nèi)聯(lián)約束 .................................................................................................. 711 20.1.3 可缺省路由參數(shù)............................................................................................... 712 20.1.4 特殊的路由參數(shù)............................................................................................... 713 20.1.5 主機(jī)名綁定 ...................................................................................................... 715 20.1.6 更加自由的定義方式 ....................................................................................... 716 20.2 路由分發(fā) ...................................................................................................................... 717 20.2.1 路由模式 .......................................................................................................... 717 20.2.2 路由終節(jié)點(diǎn) ...................................................................................................... 722 20.2.3 中間件.............................................................................................................. 730 20.2.4 處理器適配 ...................................................................................................... 737 20.2.5 Minimal API ..................................................................................................... 746 20.3 路由約束 ...................................................................................................................... 748 20.3.1 預(yù)定義的 IRouteConstraint ............................................................................... 749 20.3.2 IInlineConstraintResolver .................................................................................. 750 20.3.3 自定義約束 ...................................................................................................... 750 第 21 章 異常處理 ..................................................................................................................... 753 21.1 呈現(xiàn)錯(cuò)誤信息 .............................................................................................................. 753 21.1.1 開(kāi)發(fā)者異常頁(yè)面 .............................................................................................. 753 21.1.2 定制異常頁(yè)面 .................................................................................................. 756 21.1.3 針對(duì)響應(yīng)狀態(tài)碼定制錯(cuò)誤頁(yè)面 ........................................................................ 757 21.2 開(kāi)發(fā)者異常頁(yè)面 ........................................................................................................... 759 21.2.1 IDeveloperPageExceptionFilter ......................................................................... 759 21.2.2 顯示編譯異常信息 ........................................................................................... 760 21.2.3 DeveloperExceptionPageMiddleware ................................................................ 764 21.3 異常處理器 .................................................................................................................. 766 21.3.1 ExceptionHandlerMiddleware ........................................................................... 766 21.3.2 IExceptionHandlerPathFeature 特性 ................................................................. 768 21.3.3 清除緩存 .......................................................................................................... 771 21.3.4 404 響應(yīng) ........................................................................................................... 772 21.4 響應(yīng)狀態(tài)碼頁(yè)面 ........................................................................................................... 774 21.4.1 StatusCodePagesMiddleware............................................................................. 774 21.4.2 阻止處理異常 .................................................................................................. 775 21.4.3 注冊(cè)中間件 ...................................................................................................... 777 第 22 章 響應(yīng)緩存 ..................................................................................................................... 783 22.1 緩存響應(yīng)內(nèi)容 .............................................................................................................. 783 22.1.1 基于路徑的響應(yīng)緩存 ....................................................................................... 783 22.1.2 引入其他緩存維度 ........................................................................................... 785 22.1.3 緩存屏蔽 .......................................................................................................... 790 22.2 HTTP-Cache ................................................................................................................. 791 22.2.1 私有緩存和共享緩存 ....................................................................................... 792 22.2.2 響應(yīng)的提取 ...................................................................................................... 792 22.2.3 新鮮度檢驗(yàn) ...................................................................................................... 793 22.2.4 顯式緩存控制 .................................................................................................. 794 22.3 中間件 .......................................................................................................................... 795 22.3.1 緩存上下文對(duì)象............................................................................................... 795 22.3.2 緩存策略 .......................................................................................................... 796 22.3.3 緩存鍵 .............................................................................................................. 797 22.3.4 緩存的讀/寫(xiě) ..................................................................................................... 798 22.3.5 ResponseCachingMiddleware ............................................................................ 799 22.3.6 注冊(cè)中間件 ...................................................................................................... 801 第 23 章 會(huì)話(huà) ............................................................................................................................. 803 23.1 利用會(huì)話(huà)保留“語(yǔ)境” ............................................................................................... 803 23.1.1 設(shè)置和提取會(huì)話(huà)狀態(tài) ....................................................................................... 803 23.1.2 查看存儲(chǔ)的會(huì)話(huà)狀態(tài) ....................................................................................... 805 23.1.3 查看 Cookie ...................................................................................................... 807 23.2 會(huì)話(huà)狀態(tài)的讀/寫(xiě) .......................................................................................................... 807 23.2.1 ISession ............................................................................................................ 808 23.2.2 DistributedSession ............................................................................................ 809 23.2.3 ISessionStore .................................................................................................... 810 23.3 會(huì)話(huà)中間件 .................................................................................................................. 811 23.3.1 SessionOptions ................................................................................................. 811 23.3.2 ISessionFeature ................................................................................................. 812 23.3.3 SessionMiddleware ........................................................................................... 812 第 24 章 HTTPS 策略 ............................................................................................................... 815 24.1 HTTPS 終節(jié)點(diǎn)的切換 .................................................................................................. 815 24.1.1 構(gòu)建 HTTPS 站點(diǎn) ............................................................................................ 815 24.1.2 HTTPS 重定向 ................................................................................................. 818 24.1.3 瀏覽器自動(dòng)重定向 ........................................................................................... 819 24.1.4 HSTS 選項(xiàng)配置 ................................................................................................ 821 24.2 HTTPS 重定向 ............................................................................................................. 823 24.2.1 HttpsRedirectionOptions ................................................................................... 823 24.2.2 HttpsRedirectionMiddleware 中間件 ................................................................ 824 24.2.3 中間件注冊(cè) ...................................................................................................... 826 24.3 HSTS ............................................................................................................................ 827 24.3.1 HstsOptions ...................................................................................................... 827 24.3.2 HstsMiddleware 中間件 .................................................................................... 828 24.3.3 中間件注冊(cè) ...................................................................................................... 829 第 25 章 重定向 ......................................................................................................................... 830 25.1 基于規(guī)則的重定向 ....................................................................................................... 830 25.1.1 客戶(hù)端重定向 .................................................................................................. 830 25.1.2 服務(wù)端重定向 .................................................................................................. 831 25.1.3 IIS 重寫(xiě)規(guī)則 .................................................................................................... 832 25.1.4 Apache 重寫(xiě)規(guī)則 ............................................................................................ 834 25.1.5 HTTPS 重定向 ................................................................................................. 836 25.2 重定向中間件 .............................................................................................................. 838 25.2.1 重定向規(guī)則 ...................................................................................................... 838 25.2.2 RewriteMiddleware ........................................................................................... 839 25.3 預(yù)定義規(guī)則 .................................................................................................................. 841 25.3.1 “萬(wàn)能”規(guī)則 .................................................................................................. 841 25.3.2 客戶(hù)端重定向 .................................................................................................. 842 25.3.3 服務(wù)端重定向 .................................................................................................. 843 25.3.4 WWW 重定向 .................................................................................................. 843 25.3.5 HTTPS 重定向 ................................................................................................. 845 25.3.6 IIS 重寫(xiě)規(guī)則 .................................................................................................... 846 25.3.7 Apache 重寫(xiě)規(guī)則 ............................................................................................. 848 第 26 章 限流............................................................................................................................. 850 26.1 控制并發(fā)量 .................................................................................................................. 850 26.1.1 設(shè)置并發(fā)和等待請(qǐng)求閾值 ............................................................................... 850 26.1.2 初識(shí)基于隊(duì)列的處理策略 ............................................................................... 853 26.1.3 初識(shí)基于棧的處理策略 ................................................................................... 855 26.2 并發(fā)限制中間件 ........................................................................................................... 856 26.2.1 等待隊(duì)列策略 .................................................................................................. 856 26.2.2 ConcurrencyLimiterMiddleware ........................................................................ 856 26.2.3 處理拒絕請(qǐng)求 .................................................................................................. 858 26.3 等待隊(duì)列策略 ............................................................................................................... 859 26.3.1 基于隊(duì)列的處理策略 ....................................................................................... 859 26.3.2 基于棧的處理策略 ........................................................................................... 861 第 27 章 認(rèn)證 ............................................................................................................................. 867 27.1 認(rèn)證、登錄與注銷(xiāo) ....................................................................................................... 867 27.1.1 認(rèn)證票據(jù) .......................................................................................................... 867 27.1.2 基于 Cookie 的認(rèn)證 ......................................................................................... 868 27.1.3 強(qiáng)制認(rèn)證 .......................................................................................................... 871 27.1.4 登錄與注銷(xiāo) ...................................................................................................... 871 27.2 身份與用戶(hù) .................................................................................................................. 872 27.2.1 IIdentity ............................................................................................................ 872 27.2.2 IPrincipal .......................................................................................................... 877 27.3 認(rèn)證模型 ...................................................................................................................... 880 27.3.1 認(rèn)證票據(jù)詳細(xì)介紹 ........................................................................................... 880 27.3.2 認(rèn)證處理器 ...................................................................................................... 883 27.3.3 認(rèn)證服務(wù) .......................................................................................................... 889 27.3.4 服務(wù)注冊(cè) .......................................................................................................... 893 27.3.5 AuthenticationMiddleware ................................................................................ 896 27.4 Cookie 認(rèn)證方案 .......................................................................................................... 898 27.4.1 AuthenticationHandler<TOptions> .................................................................... 898 27.4.2 CookieAuthenticationHandler ........................................................................... 903 27.4.3 注冊(cè) CookieAuthenticationHandler ................................................................... 912 第 28 章 授權(quán) ............................................................................................................................. 915 28.1 基于“角色”的授權(quán) ................................................................................................... 915 28.1.1 基于“要求”的授權(quán) ....................................................................................... 915 28.1.2 預(yù)定義授權(quán)策略............................................................................................... 919 28.1.3 基于終節(jié)點(diǎn)的自動(dòng)化授權(quán) ............................................................................... 921 28.2 基于“要求”的授權(quán) ................................................................................................... 922 28.2.1 IAuthorizationHandler ...................................................................................... 922 28.2.2 預(yù)定義授權(quán)處理器 ........................................................................................... 924 28.2.3 授權(quán)檢驗(yàn) .......................................................................................................... 928 28.3 基于“策略”的授權(quán) ................................................................................................... 933 28.3.1 授權(quán)策略的構(gòu)建 .............................................................................................. 934 28.3.2 授權(quán)策略的注冊(cè) .............................................................................................. 935 28.3.3 授權(quán)檢驗(yàn) .......................................................................................................... 936 28.4 授權(quán)與路由 .................................................................................................................. 937 28.4.1 IAuthorizeData ................................................................................................. 937 28.4.2 IAllowAnonymous ............................................................................................ 939 28.4.3 IPolicyEvaluator ............................................................................................... 940 28.4.4 IAuthorizationMiddlewareResultHandler .......................................................... 943 28.4.5 AuthorizationMiddleware .................................................................................. 944 第 29 章 跨域資源共享 ............................................................................................................. 947 29.1 處理跨域資源 .............................................................................................................. 947 29.1.1 跨域調(diào)用 API ................................................................................................... 947 29.1.2 提供者顯式授權(quán) .............................................................................................. 950 29.1.3 基于策略的資源授權(quán) ....................................................................................... 951 29.1.4 將 CORS 策略應(yīng)用到路由上 ........................................................................... 952 29.2 CORS 規(guī)范 ................................................................................................................... 953 29.2.1 同源策略 .......................................................................................................... 953 29.2.2 針對(duì)資源的授權(quán) .............................................................................................. 954 29.2.3 獲取授權(quán)的方式 .............................................................................................. 955 29.2.4 用戶(hù)憑證 .......................................................................................................... 958 29.3 CORS 中間件 ............................................................................................................... 958 29.3.1 CORS 策略 ...................................................................................................... 958 29.3.2 CORS 與路由 ................................................................................................... 961 29.3.3 CORS 授權(quán) ...................................................................................................... 962 29.3.4 CorsMiddleware ............................................................................................... 964 第 30 章 健康檢查 ..................................................................................................................... 968 30.1 檢查應(yīng)用的健康狀況 ................................................................................................... 968 30.1.1 確定當(dāng)前應(yīng)用是否可用 ................................................................................... 968 30.1.2 定制健康檢查邏輯 ........................................................................................... 969 30.1.3 改變響應(yīng)狀態(tài)碼............................................................................................... 970 30.1.4 細(xì)粒度的健康檢查 ........................................................................................... 972 30.1.5 定制響應(yīng)內(nèi)容 .................................................................................................. 973 30.1.6 過(guò)濾 IHealthCheck 對(duì)象 ................................................................................... 975 30.2 設(shè)計(jì)與實(shí)現(xiàn) .................................................................................................................. 975 30.2.1 IHealthCheck .................................................................................................... 976 30.2.2 HealthCheckService .......................................................................................... 981 30.2.3 HealthCheckMiddleware ................................................................................... 984 30.3 發(fā)布健康報(bào)告 ............................................................................................................... 989 30.3.1 定期發(fā)布健康報(bào)告 ........................................................................................... 989 30.3.2 IHealthCheckPublisher ...................................................................................... 990 30.3.3 HealthCheckPublisherHostedService ................................................................. 991
你還可能感興趣
我要評(píng)論
|