From 3ff8983bd06d29bddcdd50168f75de092c770711 Mon Sep 17 00:00:00 2001
From: wanggaofeng <15601716045@163.com>
Date: Tue, 23 Jul 2024 20:08:00 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../WebApiConfigExtensions.cs                 |  2 +-
 HttpClientStudy.Config/WebApiConfigManager.cs |  2 +-
 .../ConfigTest/WebApiConfigTest.cs            |  2 +-
 .../Controllers/ConfigController.cs           | 39 ++++++++++
 .../HttpClientStudy.WebApp.csproj             |  2 +-
 .../Controllers/CallApiController.cs          | 17 +++++
 .../Controllers/ManagerController.cs          |  5 ++
 .../HttpClientStudy.WebClient.csproj          |  2 +
 HttpClientStudy.WebClient/Program.cs          | 75 +++++++++++++++++--
 9 files changed, 137 insertions(+), 9 deletions(-)
 create mode 100644 HttpClientStudy.WebApp/Controllers/ConfigController.cs

diff --git a/HttpClientStudy.Config/WebApiConfigExtensions.cs b/HttpClientStudy.Config/WebApiConfigExtensions.cs
index 23c45d5..3c86976 100644
--- a/HttpClientStudy.Config/WebApiConfigExtensions.cs
+++ b/HttpClientStudy.Config/WebApiConfigExtensions.cs
@@ -51,7 +51,7 @@ namespace HttpClientStudy.Config
         /// </summary>
         /// <param name="serviceProvider"></param>
         /// <returns></returns>
-        public static IServiceCollection AddWebApiOptions(this IServiceCollection services)
+        public static IServiceCollection UseWebApiOptions(this IServiceCollection services)
         {
             services.AddOptions();
 
diff --git a/HttpClientStudy.Config/WebApiConfigManager.cs b/HttpClientStudy.Config/WebApiConfigManager.cs
index 4547040..2a8be71 100644
--- a/HttpClientStudy.Config/WebApiConfigManager.cs
+++ b/HttpClientStudy.Config/WebApiConfigManager.cs
@@ -25,7 +25,7 @@ namespace HttpClientStudy.Config
 
             ServiceCollection services = new ServiceCollection();
             services.AddSingleton<IConfiguration>(configurationBuilder.Build());
-            services.AddWebApiOptions();
+            services.UseWebApiOptions();
 
             var webApiConfigOption = services.BuildServiceProvider().GetService<IOptionsMonitor<WebApiConfig>>();
             if (webApiConfigOption == null)
diff --git a/HttpClientStudy.UnitTest/ConfigTest/WebApiConfigTest.cs b/HttpClientStudy.UnitTest/ConfigTest/WebApiConfigTest.cs
index eae1b14..c77c884 100644
--- a/HttpClientStudy.UnitTest/ConfigTest/WebApiConfigTest.cs
+++ b/HttpClientStudy.UnitTest/ConfigTest/WebApiConfigTest.cs
@@ -25,7 +25,7 @@ namespace HttpClientStudy.UnitTest.ConfigTest
 
             var services = new ServiceCollection();
             services.AddSingleton(configuration);
-            services.AddWebApiOptions();
+            services.UseWebApiOptions();
 
             var provider = services.BuildServiceProvider();
             IOptions<WebApiConfig>? webApiOptions = provider.GetService<IOptions<WebApiConfig>>();
diff --git a/HttpClientStudy.WebApp/Controllers/ConfigController.cs b/HttpClientStudy.WebApp/Controllers/ConfigController.cs
new file mode 100644
index 0000000..057e8ed
--- /dev/null
+++ b/HttpClientStudy.WebApp/Controllers/ConfigController.cs
@@ -0,0 +1,39 @@
+using HttpClientStudy.Config;
+
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
+
+namespace HttpClientStudy.WebApp.Controllers
+{
+    /// <summary>
+    /// 使用配置文件 控制器
+    /// </summary>
+    [Route("api/[controller]/[action]")]
+    [ApiController]
+    public class ConfigController : ControllerBase
+    {
+        private ILogger<SimpleController> _logger;
+        private IOptionsMonitor<WebApiConfig> _apiConfigMonitor;
+
+        /// <summary>
+        /// 构造
+        /// </summary>
+        public ConfigController(ILogger<SimpleController> logger, IOptionsMonitor<WebApiConfig> apiConfigMonitor)
+        { 
+            _logger = logger;
+            _apiConfigMonitor = apiConfigMonitor;
+        }
+
+        /// <summary>
+        /// 获取账号
+        /// </summary>
+        /// <returns></returns>
+        [HttpGet]
+        public IActionResult GetApiConfig()
+        {
+            var reslut = BaseResultUtil.Success(_apiConfigMonitor.CurrentValue);
+            return Ok(reslut);
+        }
+    }
+}
diff --git a/HttpClientStudy.WebApp/HttpClientStudy.WebApp.csproj b/HttpClientStudy.WebApp/HttpClientStudy.WebApp.csproj
index 4d8e356..172dd55 100644
--- a/HttpClientStudy.WebApp/HttpClientStudy.WebApp.csproj
+++ b/HttpClientStudy.WebApp/HttpClientStudy.WebApp.csproj
@@ -5,7 +5,7 @@
     <Nullable>enable</Nullable>
     <ImplicitUsings>enable</ImplicitUsings>
     <InvariantGlobalization>true</InvariantGlobalization>
-	  <GenerateDocumentationFile>true</GenerateDocumentationFile>
+	<GenerateDocumentationFile>true</GenerateDocumentationFile>
   </PropertyGroup>
 
   <ItemGroup>
diff --git a/HttpClientStudy.WebClient/Controllers/CallApiController.cs b/HttpClientStudy.WebClient/Controllers/CallApiController.cs
index 05fecd8..dc29ab3 100644
--- a/HttpClientStudy.WebClient/Controllers/CallApiController.cs
+++ b/HttpClientStudy.WebClient/Controllers/CallApiController.cs
@@ -5,16 +5,28 @@ using Microsoft.AspNetCore.Mvc;
 
 namespace HttpClientStudy.WebClient.Controllers
 {
+    /// <summary>
+    /// 调用API 控制器
+    /// </summary>
     [Route("api/[controller]/[action]")]
     [ApiController]
     public class CallApiController : ControllerBase
     {
         private readonly ILogger<CallApiController> _logger;
+
+        /// <summary>
+        /// 构造
+        /// </summary>
+        /// <param name="logger"></param>
         public CallApiController(ILogger<CallApiController> logger) 
         {
             _logger = logger;
         }
 
+        /// <summary>
+        /// Ping
+        /// </summary>
+        /// <returns></returns>
         [HttpGet]
         public IActionResult Ping()
         {
@@ -22,6 +34,11 @@ namespace HttpClientStudy.WebClient.Controllers
             return Ok("ping");
         }
 
+        /// <summary>
+        /// 异常测试
+        /// </summary>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
         [HttpGet]
         public IActionResult Exception()
         {
diff --git a/HttpClientStudy.WebClient/Controllers/ManagerController.cs b/HttpClientStudy.WebClient/Controllers/ManagerController.cs
index 0006b12..b73c0b4 100644
--- a/HttpClientStudy.WebClient/Controllers/ManagerController.cs
+++ b/HttpClientStudy.WebClient/Controllers/ManagerController.cs
@@ -13,6 +13,11 @@ namespace HttpClientStudy.WebClient.Controllers
     public class ManagerController : ControllerBase
     {
         private readonly ILogger<ManagerController> _logger;
+        
+        /// <summary>
+        /// 构造
+        /// </summary>
+        /// <param name="logger"></param>
         public ManagerController(ILogger<ManagerController> logger) 
         {
             _logger = logger;
diff --git a/HttpClientStudy.WebClient/HttpClientStudy.WebClient.csproj b/HttpClientStudy.WebClient/HttpClientStudy.WebClient.csproj
index 397261a..81dba61 100644
--- a/HttpClientStudy.WebClient/HttpClientStudy.WebClient.csproj
+++ b/HttpClientStudy.WebClient/HttpClientStudy.WebClient.csproj
@@ -4,10 +4,12 @@
     <TargetFramework>net8.0</TargetFramework>
     <Nullable>enable</Nullable>
     <ImplicitUsings>enable</ImplicitUsings>
+    <GenerateDocumentationFile>true</GenerateDocumentationFile>
   </PropertyGroup>
 
   <ItemGroup>
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
+    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.6.2" />
   </ItemGroup>
 
   <ItemGroup>
diff --git a/HttpClientStudy.WebClient/Program.cs b/HttpClientStudy.WebClient/Program.cs
index 1ae13f5..7fb0f10 100644
--- a/HttpClientStudy.WebClient/Program.cs
+++ b/HttpClientStudy.WebClient/Program.cs
@@ -2,6 +2,8 @@
 
 using HttpClientStudy.Core.Utilities;
 
+using Microsoft.OpenApi.Models;
+
 //启动WebApi程序
 AppUtility.StartWebApiProject();
 
@@ -12,16 +14,79 @@ var builder = WebApplication.CreateBuilder(args);
 builder.Services.AddControllers();
 // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
 builder.Services.AddEndpointsApiExplorer();
-builder.Services.AddSwaggerGen();
+//配置Swagger
+builder.Services.AddSwaggerGen(setup =>
+{
+    #region 定义Swagger文档
+    //name参数即为SwaggerUI中SwaggerEndpoint方法参数中的{documentName}
+    //两者必须保持一致,否则异常
+    setup.SwaggerDoc(name: "v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "HttpClient学习", Version = "第1版" });
+    #endregion
+
+    #region 包含xml注释
+    var xmlCommentFiles = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "HttpClientStudy.*.xml", System.IO.SearchOption.TopDirectoryOnly);
+    foreach (var xmlFile in xmlCommentFiles)
+    {
+        //includeControllerXmlComments参数:是否启用控制器上的xml注释
+        setup.IncludeXmlComments(filePath: xmlFile, includeControllerXmlComments: true);
+
+        setup.UseInlineDefinitionsForEnums();
+    }
+    #endregion
+
+    /*
+    #region 放置接口Auth授权按钮
+
+    setup.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
+    {
+        Description = "请输入带有Bearer的Token:Bearer {Token}",
+
+        //jwt默认的参数名称
+        Name = "Authorization",
+
+        //jwt默认存放 Authorization 信息的位置:此处为请求头中
+        In = ParameterLocation.Header,
+
+        //验证类型:此处使用Api Key
+        Type = SecuritySchemeType.ApiKey
+    });
+    
+    #endregion
+
+    #region 指定方案应用范围
+    setup.AddSecurityRequirement(new OpenApiSecurityRequirement
+    {
+        {
+            new OpenApiSecurityScheme
+            {
+                Reference = new OpenApiReference
+                {
+                    Id = "Bearer",
+                    Type = ReferenceType.SecurityScheme
+                }
+            },
+            new List<string>()
+        }
+    });
+    #endregion
+    */
+
+    //启用数据注解
+    setup.EnableAnnotations();
+});
 
 var app = builder.Build();
 
 // Configure the HTTP request pipeline.
-if (app.Environment.IsDevelopment())
+app.UseSwagger();
+app.UseSwaggerUI(setup =>
 {
-    app.UseSwagger();
-    app.UseSwaggerUI();
-}
+    setup.EnableDeepLinking();
+    setup.DisplayRequestDuration();
+    setup.ShowCommonExtensions();
+    setup.ShowExtensions();
+    setup.EnableFilter();
+});
 
 app.UseAuthorization();