关于我们
书单推荐
新书推荐
|
ASP.NET Core 6框架揭秘(上下册) 读者对象:对ASP.NET MVC编程模式具有基本了解的.NET开发人员。
本书主要介绍ASP.NET Core框架最核心的部分,即由一个服务器和若干中间件构建的管道。本书共分为5篇:"第1篇 初识编程(第1章)”列举一系列极简的实例为读者提供基本的编程体验,"第2 篇 基础框架(第2~13章)”主要介绍了一系列支撑ASP.NET Core的基础框架,"第3篇 承载系统(第14~17章)”主要介绍了ASP.NET Core应用的承载流程,"第4篇 服务器概述(第18章)”列举一系列常见的服务器类型并对它们进行了比较,"第5 篇 中间件(第19~30章)”系统地介绍了一系列预定义的中间件。
蒋金楠,网名Artech,高级软件顾问。微软6任MVP(Solutions Architecture、Connected System与Microsoft Integration)。中国 .NET 技术领域资深专家,著有《WCF全面解析》)、《ASP.NET MVC 5框架揭秘》等多部领域畅销著作。对.NET Framework、C#、ASP.NET、SQL Server、设计模式、软件架构,以及主流开源框架有着深入研究,同时对.NET Remoting、MSMQ通信技术有深入的理解。博客园头牌博主,51CTO十佳博客,知名技术公众号“大内老A”号主。
第 1 篇 初识编程
第 1 章 编程体验 ........................................................................................................................... 2 1.1 控制台程序 ........................................................................................................................ 2 1.1.1 构建开发环境 ........................................................................................................ 2 1.1.2 命令行构建 .NET 应用 ......................................................................................... 3 1.2 ASP.NET 应用 .................................................................................................................... 8 1.2.1 launchSettings.json ................................................................................................. 8 1.2.2 Minimal API ......................................................................................................... 11 1.2.3 中间件 ................................................................................................................. 13 1.2.4 配置选项 .............................................................................................................. 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 定义服务 .............................................................................................................. 24 1.4.2 实现和承载 .......................................................................................................... 26 1.4.3 调用服务 .............................................................................................................. 28 1.5 Dapr.................................................................................................................................. 29 1.5.1 构建开发环境 ...................................................................................................... 29 1.5.2 服务调用 .............................................................................................................. 30 1.5.3 状态管理 .............................................................................................................. 34 1.5.4 发布订阅 .............................................................................................................. 38 1.5.5 Actor 模型 ............................................................................................................ 40 第 2 篇 基础框架 第 2 章 依赖注入(上) ............................................................................................................. 44 2.1 控制反转 .......................................................................................................................... 44 2.1.1 流程控制的反转 .................................................................................................. 44 2.1.2 好莱坞法则 .......................................................................................................... 47 2.1.3 流程定制 .............................................................................................................. 47 2.2 IoC 模式 ........................................................................................................................... 48 2.2.1 模板方法 .............................................................................................................. 48 2.2.2 工厂方法 .............................................................................................................. 49 2.2.3 抽象工厂 .............................................................................................................. 51 2.3 依赖注入 .......................................................................................................................... 53 2.3.1 由容器提供对象 .................................................................................................. 53 2.3.2 3 种注入方式 ....................................................................................................... 55 2.3.3 Service Locator 模式 ............................................................................................ 57 2.4 一个简易版的依赖注入容器 ............................................................................................ 59 2.4.1 编程体验 .............................................................................................................. 59 2.4.2 设计与实现 .......................................................................................................... 63 2.4.3 扩展方法 .............................................................................................................. 69 第 3 章 依赖注入(下) ............................................................................................................. 73 3.1 利用容器提供服务 ........................................................................................................... 73 3.1.1 服务的注册与消费 ............................................................................................... 73 3.1.2 生命周期 .............................................................................................................. 76 3.1.3 服务注册的验证 .................................................................................................. 79 3.2 服务注册 .......................................................................................................................... 82 3.2.1 ServiceDescriptor .................................................................................................. 82 3.2.2 IServiceCollection ................................................................................................. 83 3.3 服务的消费 ...................................................................................................................... 87 3.3.1 IServiceProvider ................................................................................................... 87 3.3.2 服务实例的创建 .................................................................................................. 89 3.3.3 生命周期 .............................................................................................................. 91 3.3.4 ActivatorUtilities .................................................................................................. 96 3.4 扩展................................................................................................................................ 101 3.4.1 适配 ................................................................................................................... 101 3.4.2 IServiceProviderFactory<TContainerBuilder> ..................................................... 102 3.4.3 整合第三方依赖注入框架 ................................................................................. 102 第 4 章 文件系统 ....................................................................................................................... 107 4.1 抽象的文件系统 ............................................................................................................. 107 4.1.1 树形层次结构 .................................................................................................... 107 4.1.2 读取文件内容 .................................................................................................... 109 4.1.3 监控文件的变化 ................................................................................................ 111 4.2 文件与目录 .................................................................................................................... 112 4.2.1 IChangeToken ..................................................................................................... 113 4.2.2 IFileProvider ....................................................................................................... 114 4.2.3 两个特殊的文件系统 ......................................................................................... 116 4.3 物理文件系统 ................................................................................................................ 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 内嵌文件系统 ................................................................................................................ 123 4.4.1 将项目文件变成内嵌资源 ................................................................................. 123 4.4.2 读取资源文件 .................................................................................................... 124 4.4.3 EmbeddedFileProvider ........................................................................................ 125 第 5 章 配置选项(上) ........................................................................................................... 128 5.1 读取配置信息 ................................................................................................................ 128 5.1.1 编程模型三要素 ................................................................................................ 128 5.1.2 以“键-值”对的形式读取配置 ........................................................................ 129 5.1.3 读取结构化的配置 ............................................................................................ 130 5.1.4 将结构化配置直接绑定为对象 .......................................................................... 133 5.1.5 将配置定义在文件中 ......................................................................................... 133 5.1.6 根据环境动态加载配置文件 ............................................................................. 134 5.1.7 配置内容的同步 ................................................................................................ 136 5.2 配置模型 ........................................................................................................................ 137 5.2.1 数据结构及其转换 ............................................................................................. 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 绑定配置项的值 ................................................................................................ 145 5.3.2 绑定复合对象 .................................................................................................... 147 5.3.3 绑定集合 ............................................................................................................ 149 5.3.4 绑定字典 ............................................................................................................ 151 5.4 配置的同步 .................................................................................................................... 152 5.4.1 配置数据流 ........................................................................................................ 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 章 配置选项(下) ........................................................................................................... 177 6.1 Options 模式 ................................................................................................................... 177 6.1.1 将配置绑定为 Options 对象 ............................................................................... 177 6.1.2 提供具名的 Options ........................................................................................... 179 6.1.3 配置源的同步 .................................................................................................... 180 6.1.4 直接初始化 Options 对象 ................................................................................... 182 6.1.5 根据依赖服务的 Options 设置 ........................................................................... 184 6.1.6 验证 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 依赖注入 ........................................................................................................................ 200 6.3.1 服务注册 ............................................................................................................ 200 6.3.2 IOptions<TOptions>与 IOptionsSnapshot<TOptions> ......................................... 206 6.3.3 集成配置系统 .................................................................................................... 208 第 7 章 诊断日志(上) ........................................................................................................... 210 7.1 各种诊断日志形式 ......................................................................................................... 210 7.1.1 调试日志 ............................................................................................................ 210 7.1.2 跟踪日志 ............................................................................................................ 211 7.1.3 事件日志 ............................................................................................................ 213 7.1.4 诊断日志 ............................................................................................................ 216 7.2 Debugger 调试日志 ........................................................................................................ 219 7.2.1 Debugger ............................................................................................................ 220 7.2.2 Debug ................................................................................................................. 221 7.3 TraceSource 跟踪日志 .................................................................................................... 222 7.3.1 跟踪日志模型三要素 ......................................................................................... 222 7.3.2 预定义 TraceListener .......................................................................................... 231 7.3.3 Trace .................................................................................................................. 235 7.4 EventSource 事件日志 .................................................................................................... 237 7.4.1 EventSource ....................................................................................................... 237 7.4.2 EventListener ...................................................................................................... 243 7.4.3 活动跟踪 ............................................................................................................ 247 7.5 DiagnosticSource 诊断日志 ............................................................................................ 251 7.5.1 标准的观察者模式 ............................................................................................ 251 7.5.2 AnonymousObserver<T> .................................................................................... 253 7.5.3 强类型的日志事件订阅 ..................................................................................... 256 7.5.4 针对活动的跟踪 ................................................................................................ 258 第 8 章 诊断日志(中) ........................................................................................................... 260 8.1 统一日志编程模式 ......................................................................................................... 260 8.1.1 日志输出 ............................................................................................................ 260 8.1.2 日志过滤 ............................................................................................................ 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 调用链跟踪 ........................................................................................................ 283 8.3.2 服务范围堆栈 .................................................................................................... 294 8.3.3 Activity 的应用 .................................................................................................. 296 8.4 依赖注入 ........................................................................................................................ 297 8.4.1 核心服务 ............................................................................................................ 298 8.4.2 配置 ................................................................................................................... 300 8.4.3 日志过滤规则 .................................................................................................... 303 第 9 章 诊断日志(下) ........................................................................................................... 306 9.1 控制台 ............................................................................................................................ 306 9.1.1 ConsoleFormatter ................................................................................................ 306 9.1.2 ConsoleLogger .................................................................................................... 313 9.1.3 ConsoleLoggerProvider ....................................................................................... 315 9.1.4 服务注册 ............................................................................................................ 316 9.2 调试器 ............................................................................................................................ 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 章 对象池 ......................................................................................................................... 338 10.1 利用对象池复用对象 ................................................................................................... 338 10.1.1 对象的“借”与“还” ................................................................................... 338 10.1.2 依赖注入 .......................................................................................................... 340 10.1.3 池化对象策略 .................................................................................................. 340 10.1.4 对象池的大小 .................................................................................................. 341 10.1.5 对象的释放 ...................................................................................................... 344 10.2 池化对象管理 .............................................................................................................. 345 10.2.1 IPooledObjectPolicy<T> ................................................................................... 345 10.2.2 ObjectPool<T> ................................................................................................. 346 10.2.3 ObjectPoolProvider ........................................................................................... 351 10.3 扩展应用 ...................................................................................................................... 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 将数据缓存起来 ........................................................................................................... 359 11.1.1 将数据缓存在内存中 ....................................................................................... 359 11.1.2 将数据缓存在 Redis 中 .................................................................................... 360 11.1.3 将数据缓存在 SQL Server 中 ........................................................................... 363 11.2 本地内存缓存 ............................................................................................................... 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 调用 ................................................................................................................. 385 12.1 HttpClient 的工厂 ......................................................................................................... 385 12.1.1 手动创建 HttpClient ......................................................................................... 385 12.1.2 使用 IHttpClientFactory 工厂 ........................................................................... 387 12.1.3 直接注入 HttpClient ......................................................................................... 387 12.1.4 定制 HttpClient................................................................................................. 388 12.1.5 强类型客户端 .................................................................................................. 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 复用 HttpClientHandler .................................................................................... 398 12.3 HttpClient 的构建 ......................................................................................................... 400 12.3.1 HttpMessageHandlerBuilder ............................................................................. 400 12.3.2 HttpClientFactoryOptions ................................................................................. 401 12.3.3 IHttpMessageHandlerBuilderFilter .................................................................... 407 12.3.4 IHttpClientFactory ............................................................................................ 408 12.4 依赖注入 ...................................................................................................................... 418 12.4.1 基础服务注册 .................................................................................................. 418 12.4.2 定制 HttpClient................................................................................................. 420 12.4.3 强类型客户端 .................................................................................................. 424 第 13 章 数据保护 ..................................................................................................................... 430 13.1 加密与哈希 .................................................................................................................. 430 13.1.1 数据加密与解密............................................................................................... 430 13.1.2 设置加密内容的有效期 ................................................................................... 433 13.1.3 撤销密钥 .......................................................................................................... 434 13.1.4 “瞬时”加解密............................................................................................... 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 依赖注入 ...................................................................................................................... 472 13.4.1 注册基础服务 .................................................................................................. 472 13.4.2 密钥管理配置 .................................................................................................. 477 13.4.3 扩展配置 .......................................................................................................... 478 附录 A ........................................................................................................................................... 482 第 3 篇 承载系统 第 14 章 服务承载 ..................................................................................................................... 488 14.1 服务承载 ...................................................................................................................... 488 14.1.1 性能指标收集服务 ........................................................................................... 488 14.1.2 依赖注入 .......................................................................................................... 490 14.1.3 配置选项 .......................................................................................................... 493 14.1.4 承载环境 .......................................................................................................... 496 14.1.5 日志 ................................................................................................................. 498 14.2 服务承载模型 .............................................................................................................. 500 14.2.1 IHostedService .................................................................................................. 500 14.2.2 IHost ................................................................................................................ 501 14.2.3 IHostBuilder ..................................................................................................... 506 14.3 服务承载流程 .............................................................................................................. 511 14.3.1 服务宿主 .......................................................................................................... 512 14.3.2 服务承载设置 .................................................................................................. 516 14.3.3 创建宿主 .......................................................................................................... 521 14.3.4 静态类型 Host .................................................................................................. 527 第 15 章 应用承载(上) ......................................................................................................... 531 15.1 管道式的请求处理 ....................................................................................................... 531 15.1.1 承载方式的变迁 .............................................................................................. 531 15.1.2 中间件.............................................................................................................. 536 15.1.3 定义强类型中间件 ........................................................................................... 539 15.1.4 按照约定定义中间件 ....................................................................................... 540 15.2 依赖注入 ...................................................................................................................... 542 15.2.1 服务注册 .......................................................................................................... 542 15.2.2 服务注入 .......................................................................................................... 544 15.2.3 生命周期 .......................................................................................................... 546 15.3 配置 .............................................................................................................................. 551 15.3.1 初始化配置 ...................................................................................................... 551 15.3.2 以“键-值”对形式读取和修改配置 ............................................................... 552 15.3.3 注册配置源 ...................................................................................................... 553 15.4 承载环境 ...................................................................................................................... 554 15.4.1 IWebHostEnvironment ...................................................................................... 554 15.4.2 通过配置定制承载环境 ................................................................................... 556 15.4.3 设置监听地址 .................................................................................................. 559 15.4.4 针对环境的编程............................................................................................... 560 第 16 章 应用承载(中) ......................................................................................................... 564 16.1 中间件委托链 ............................................................................................................... 564 16.1.1 HttpContext ...................................................................................................... 564 16.1.2 中间件 .............................................................................................................. 565 16.1.3 中间件管道的构建 ........................................................................................... 566 16.2 服务器 .......................................................................................................................... 567 16.2.1 IServer .............................................................................................................. 568 16.2.2 针对服务器的适配 ........................................................................................... 568 16.2.3 HttpListenerServer ............................................................................................ 570 16.3 承载服务 ...................................................................................................................... 573 16.3.1 WebHostedService ............................................................................................ 573 16.3.2 WebHostBuilder ................................................................................................ 573 16.3.3 应用构建 .......................................................................................................... 575 第 17 章 应用承载(下) ......................................................................................................... 577 17.1 共享上下文对象 ........................................................................................................... 577 17.1.1 HttpContext ...................................................................................................... 577 17.1.2 服务器适配 ...................................................................................................... 580 17.1.3 获取上下文对象 .............................................................................................. 583 17.1.4 上下文对象的创建与释放 ............................................................................... 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 弱类型中间件 .................................................................................................. 599 17.3.3 强类型中间件 .................................................................................................. 602 17.3.4 注册中间件 ...................................................................................................... 604 17.4 应用的承载 .................................................................................................................. 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 的创建 .................................................................................... 622 17.5.3 工厂方法 .......................................................................................................... 631 第 4 篇 服务器概述 第 18 章 服务器 ......................................................................................................................... 634 18.1 自定义服务器 .............................................................................................................. 634 18.1.1 IServer .............................................................................................................. 634 18.1.2 请求和响应特性 .............................................................................................. 637 18.1.3 StreamBodyFeature ........................................................................................... 638 18.1.4 HttpListenerServer ............................................................................................ 638 18.2 KestrelServer ................................................................................................................ 642 18.2.1 注册终节点 ...................................................................................................... 642 18.2.2 限制约束 .......................................................................................................... 652 18.2.3 其他设置 .......................................................................................................... 655 18.2.4 设计与实现 ...................................................................................................... 655 18.3 HTTP.SYS .................................................................................................................... 668 18.3.1 HTTP.SYS 简介 ................................................................................................ 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 章 静态文件 ..................................................................................................................... 682 19.1 搭建文件服务器 ........................................................................................................... 682 19.1.1 发布物理文件 .................................................................................................. 682 19.1.2 呈现目录结构 .................................................................................................. 684 19.1.3 显示默认页面 .................................................................................................. 685 19.1.4 映射媒体类型 .................................................................................................. 688 19.2 处理文件请求 ............................................................................................................... 690 19.2.1 条件请求 .......................................................................................................... 690 19.2.2 区间请求 .......................................................................................................... 694 19.2.3 StaticFileMiddleware ........................................................................................ 696 19.3 处理目录请求 ............................................................................................................... 700 19.3.1 DirectoryBrowserMiddleware ........................................................................... 700 19.3.2 DefaultFilesMiddleware .................................................................................... 704 第 20 章 路由 ............................................................................................................................. 708 20.1 路由映射 ...................................................................................................................... 708 20.1.1 注册终节点 ...................................................................................................... 708 20.1.2 设置内联约束 .................................................................................................. 711 20.1.3 可缺省路由参数............................................................................................... 712 20.1.4 特殊的路由参数............................................................................................... 713 20.1.5 主机名绑定 ...................................................................................................... 715 20.1.6 更加自由的定义方式 ....................................................................................... 716 20.2 路由分发 ...................................................................................................................... 717 20.2.1 路由模式 .......................................................................................................... 717 20.2.2 路由终节点 ...................................................................................................... 722 20.2.3 中间件.............................................................................................................. 730 20.2.4 处理器适配 ...................................................................................................... 737 20.2.5 Minimal API ..................................................................................................... 746 20.3 路由约束 ...................................................................................................................... 748 20.3.1 预定义的 IRouteConstraint ............................................................................... 749 20.3.2 IInlineConstraintResolver .................................................................................. 750 20.3.3 自定义约束 ...................................................................................................... 750 第 21 章 异常处理 ..................................................................................................................... 753 21.1 呈现错误信息 .............................................................................................................. 753 21.1.1 开发者异常页面 .............................................................................................. 753 21.1.2 定制异常页面 .................................................................................................. 756 21.1.3 针对响应状态码定制错误页面 ........................................................................ 757 21.2 开发者异常页面 ........................................................................................................... 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 响应 ........................................................................................................... 772 21.4 响应状态码页面 ........................................................................................................... 774 21.4.1 StatusCodePagesMiddleware............................................................................. 774 21.4.2 阻止处理异常 .................................................................................................. 775 21.4.3 注册中间件 ...................................................................................................... 777 第 22 章 响应缓存 ..................................................................................................................... 783 22.1 缓存响应内容 .............................................................................................................. 783 22.1.1 基于路径的响应缓存 ....................................................................................... 783 22.1.2 引入其他缓存维度 ........................................................................................... 785 22.1.3 缓存屏蔽 .......................................................................................................... 790 22.2 HTTP-Cache ................................................................................................................. 791 22.2.1 私有缓存和共享缓存 ....................................................................................... 792 22.2.2 响应的提取 ...................................................................................................... 792 22.2.3 新鲜度检验 ...................................................................................................... 793 22.2.4 显式缓存控制 .................................................................................................. 794 22.3 中间件 .......................................................................................................................... 795 22.3.1 缓存上下文对象............................................................................................... 795 22.3.2 缓存策略 .......................................................................................................... 796 22.3.3 缓存键 .............................................................................................................. 797 22.3.4 缓存的读/写 ..................................................................................................... 798 22.3.5 ResponseCachingMiddleware ............................................................................ 799 22.3.6 注册中间件 ...................................................................................................... 801 第 23 章 会话 ............................................................................................................................. 803 23.1 利用会话保留“语境” ............................................................................................... 803 23.1.1 设置和提取会话状态 ....................................................................................... 803 23.1.2 查看存储的会话状态 ....................................................................................... 805 23.1.3 查看 Cookie ...................................................................................................... 807 23.2 会话状态的读/写 .......................................................................................................... 807 23.2.1 ISession ............................................................................................................ 808 23.2.2 DistributedSession ............................................................................................ 809 23.2.3 ISessionStore .................................................................................................... 810 23.3 会话中间件 .................................................................................................................. 811 23.3.1 SessionOptions ................................................................................................. 811 23.3.2 ISessionFeature ................................................................................................. 812 23.3.3 SessionMiddleware ........................................................................................... 812 第 24 章 HTTPS 策略 ............................................................................................................... 815 24.1 HTTPS 终节点的切换 .................................................................................................. 815 24.1.1 构建 HTTPS 站点 ............................................................................................ 815 24.1.2 HTTPS 重定向 ................................................................................................. 818 24.1.3 浏览器自动重定向 ........................................................................................... 819 24.1.4 HSTS 选项配置 ................................................................................................ 821 24.2 HTTPS 重定向 ............................................................................................................. 823 24.2.1 HttpsRedirectionOptions ................................................................................... 823 24.2.2 HttpsRedirectionMiddleware 中间件 ................................................................ 824 24.2.3 中间件注册 ...................................................................................................... 826 24.3 HSTS ............................................................................................................................ 827 24.3.1 HstsOptions ...................................................................................................... 827 24.3.2 HstsMiddleware 中间件 .................................................................................... 828 24.3.3 中间件注册 ...................................................................................................... 829 第 25 章 重定向 ......................................................................................................................... 830 25.1 基于规则的重定向 ....................................................................................................... 830 25.1.1 客户端重定向 .................................................................................................. 830 25.1.2 服务端重定向 .................................................................................................. 831 25.1.3 IIS 重写规则 .................................................................................................... 832 25.1.4 Apache 重写规则 ............................................................................................ 834 25.1.5 HTTPS 重定向 ................................................................................................. 836 25.2 重定向中间件 .............................................................................................................. 838 25.2.1 重定向规则 ...................................................................................................... 838 25.2.2 RewriteMiddleware ........................................................................................... 839 25.3 预定义规则 .................................................................................................................. 841 25.3.1 “万能”规则 .................................................................................................. 841 25.3.2 客户端重定向 .................................................................................................. 842 25.3.3 服务端重定向 .................................................................................................. 843 25.3.4 WWW 重定向 .................................................................................................. 843 25.3.5 HTTPS 重定向 ................................................................................................. 845 25.3.6 IIS 重写规则 .................................................................................................... 846 25.3.7 Apache 重写规则 ............................................................................................. 848 第 26 章 限流............................................................................................................................. 850 26.1 控制并发量 .................................................................................................................. 850 26.1.1 设置并发和等待请求阈值 ............................................................................... 850 26.1.2 初识基于队列的处理策略 ............................................................................... 853 26.1.3 初识基于栈的处理策略 ................................................................................... 855 26.2 并发限制中间件 ........................................................................................................... 856 26.2.1 等待队列策略 .................................................................................................. 856 26.2.2 ConcurrencyLimiterMiddleware ........................................................................ 856 26.2.3 处理拒绝请求 .................................................................................................. 858 26.3 等待队列策略 ............................................................................................................... 859 26.3.1 基于队列的处理策略 ....................................................................................... 859 26.3.2 基于栈的处理策略 ........................................................................................... 861 第 27 章 认证 ............................................................................................................................. 867 27.1 认证、登录与注销 ....................................................................................................... 867 27.1.1 认证票据 .......................................................................................................... 867 27.1.2 基于 Cookie 的认证 ......................................................................................... 868 27.1.3 强制认证 .......................................................................................................... 871 27.1.4 登录与注销 ...................................................................................................... 871 27.2 身份与用户 .................................................................................................................. 872 27.2.1 IIdentity ............................................................................................................ 872 27.2.2 IPrincipal .......................................................................................................... 877 27.3 认证模型 ...................................................................................................................... 880 27.3.1 认证票据详细介绍 ........................................................................................... 880 27.3.2 认证处理器 ...................................................................................................... 883 27.3.3 认证服务 .......................................................................................................... 889 27.3.4 服务注册 .......................................................................................................... 893 27.3.5 AuthenticationMiddleware ................................................................................ 896 27.4 Cookie 认证方案 .......................................................................................................... 898 27.4.1 AuthenticationHandler<TOptions> .................................................................... 898 27.4.2 CookieAuthenticationHandler ........................................................................... 903 27.4.3 注册 CookieAuthenticationHandler ................................................................... 912 第 28 章 授权 ............................................................................................................................. 915 28.1 基于“角色”的授权 ................................................................................................... 915 28.1.1 基于“要求”的授权 ....................................................................................... 915 28.1.2 预定义授权策略............................................................................................... 919 28.1.3 基于终节点的自动化授权 ............................................................................... 921 28.2 基于“要求”的授权 ................................................................................................... 922 28.2.1 IAuthorizationHandler ...................................................................................... 922 28.2.2 预定义授权处理器 ........................................................................................... 924 28.2.3 授权检验 .......................................................................................................... 928 28.3 基于“策略”的授权 ................................................................................................... 933 28.3.1 授权策略的构建 .............................................................................................. 934 28.3.2 授权策略的注册 .............................................................................................. 935 28.3.3 授权检验 .......................................................................................................... 936 28.4 授权与路由 .................................................................................................................. 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 跨域调用 API ................................................................................................... 947 29.1.2 提供者显式授权 .............................................................................................. 950 29.1.3 基于策略的资源授权 ....................................................................................... 951 29.1.4 将 CORS 策略应用到路由上 ........................................................................... 952 29.2 CORS 规范 ................................................................................................................... 953 29.2.1 同源策略 .......................................................................................................... 953 29.2.2 针对资源的授权 .............................................................................................. 954 29.2.3 获取授权的方式 .............................................................................................. 955 29.2.4 用户凭证 .......................................................................................................... 958 29.3 CORS 中间件 ............................................................................................................... 958 29.3.1 CORS 策略 ...................................................................................................... 958 29.3.2 CORS 与路由 ................................................................................................... 961 29.3.3 CORS 授权 ...................................................................................................... 962 29.3.4 CorsMiddleware ............................................................................................... 964 第 30 章 健康检查 ..................................................................................................................... 968 30.1 检查应用的健康状况 ................................................................................................... 968 30.1.1 确定当前应用是否可用 ................................................................................... 968 30.1.2 定制健康检查逻辑 ........................................................................................... 969 30.1.3 改变响应状态码............................................................................................... 970 30.1.4 细粒度的健康检查 ........................................................................................... 972 30.1.5 定制响应内容 .................................................................................................. 973 30.1.6 过滤 IHealthCheck 对象 ................................................................................... 975 30.2 设计与实现 .................................................................................................................. 975 30.2.1 IHealthCheck .................................................................................................... 976 30.2.2 HealthCheckService .......................................................................................... 981 30.2.3 HealthCheckMiddleware ................................................................................... 984 30.3 发布健康报告 ............................................................................................................... 989 30.3.1 定期发布健康报告 ........................................................................................... 989 30.3.2 IHealthCheckPublisher ...................................................................................... 990 30.3.3 HealthCheckPublisherHostedService ................................................................. 991
你还可能感兴趣
我要评论
|