From dee082e91615ef7abd23649532cd968543d8ee7b Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Mon, 6 Dec 2021 23:44:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 26 +++++++ .vscode/tasks.json | 42 ++++++++++++ .../Controllers/DemoController.cs | 67 +++++++++++++++++-- .../DockerStudyApi/DockerStudyApi.csproj | 3 + DockerStudyApi/DockerStudyApi/Dockerfile | 3 +- DockerStudyApi/DockerStudyApi/Program.cs | 4 ++ DockerStudyApi/DockerStudyApi/Startup.cs | 35 +++++++--- DockerStudyApi/DockerStudyApi/true | 32 +++++++++ 8 files changed, 198 insertions(+), 14 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 DockerStudyApi/DockerStudyApi/true diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..291653d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/AppDemo/AppDemo/bin/Debug/net5.0/AppDemo.dll", + "args": [], + "cwd": "${workspaceFolder}/AppDemo/AppDemo", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b427a61 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/AppDemo/AppDemo/AppDemo.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/AppDemo/AppDemo/AppDemo.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/AppDemo/AppDemo/AppDemo.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs b/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs index 90c7a6b..558512a 100644 --- a/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs +++ b/DockerStudyApi/DockerStudyApi/Controllers/DemoController.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -10,41 +11,99 @@ using System.Threading.Tasks; namespace DockerStudyApi.Controllers { + /// + /// 配置测试控制器 + /// [Route("api/[controller]/[action]")] [ApiController] public class DemoController : ControllerBase { private readonly ILogger _logger; private readonly IOptions _tespOption; + private readonly IOptionsMonitor _monitorTestOption; + private readonly IOptionsMonitorCache _monitorCacheTestOption; + private readonly IOptionsSnapshot _snapshotTestOption; - public DemoController(ILogger logger,IOptions tespOption) + private readonly IConfiguration _configuration; + + public DemoController(ILogger logger, IOptions tespOption,IOptionsMonitor monitorTestOption,IOptionsMonitorCache monitorCacheTestOption,IOptionsSnapshot snapshotTestOption, IConfiguration configuration) { _logger = logger; _tespOption = tespOption; + _monitorTestOption = monitorTestOption; + _monitorCacheTestOption = monitorCacheTestOption; + _snapshotTestOption = snapshotTestOption; + + _configuration = configuration; } + /// + /// 服务测试 + /// [HttpGet] - public IActionResult Test() + public IActionResult Ping() { return Ok(); } + /// + /// 说明:泛型左右尖括号,不能写在注释里,会生成无效的xml + /// 解决:用[]或{}或中文符号<> 代替,或者使用xml转义字符 <> + /// 获取 IOptions<TestOption> + /// [HttpGet] public TestOption TestOption() { return _tespOption.Value; } + /// + /// 获取 IOptionsMonitor<TestOptionT> + /// + [HttpGet] + public TestOption MonitorTestOption() + { + _monitorTestOption.OnChange(_ => _logger.LogWarning(_monitorTestOption.CurrentValue.AppId)); + + return _monitorTestOption.CurrentValue; + } + + /// + /// 获取 IOptionsMonitorCache<TestOption> + /// + [HttpGet] + public TestOption MonitorCacheTestOption() + { + return _monitorCacheTestOption.GetOrAdd("demo",()=> new TestOption()); + } + + /// + /// 获取 IOptionsSnapshot<TestOption> + /// + [HttpGet] + public TestOption SnapshotTestOption() + { + return _snapshotTestOption.Value; + } + + /// + /// 获取环境变量 + /// [HttpGet] public IActionResult Evn(string envName) { - return Ok(); + var envValue = _configuration.GetValue(envName); + return Ok(envValue??"无此环境变量"); } + /// + /// 获取所有配置项 + /// [HttpGet] public IActionResult Info() { - return Ok(); + var cnfigText = Newtonsoft.Json.JsonConvert.SerializeObject(_configuration.GetChildren()); + return Ok(cnfigText); } } } diff --git a/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj b/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj index 5774d7d..5de445e 100644 --- a/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj +++ b/DockerStudyApi/DockerStudyApi/DockerStudyApi.csproj @@ -2,6 +2,8 @@ net5.0 + true + $(NoWarn);1591 Linux @@ -13,6 +15,7 @@ + diff --git a/DockerStudyApi/DockerStudyApi/Dockerfile b/DockerStudyApi/DockerStudyApi/Dockerfile index a77bcbc..3a4d0d1 100644 --- a/DockerStudyApi/DockerStudyApi/Dockerfile +++ b/DockerStudyApi/DockerStudyApi/Dockerfile @@ -1,5 +1,4 @@ -#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. - +# .Net5 WebApi FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 diff --git a/DockerStudyApi/DockerStudyApi/Program.cs b/DockerStudyApi/DockerStudyApi/Program.cs index 3ee6e10..d05b9a7 100644 --- a/DockerStudyApi/DockerStudyApi/Program.cs +++ b/DockerStudyApi/DockerStudyApi/Program.cs @@ -19,6 +19,10 @@ namespace DockerStudyApi public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + //.ConfigureAppConfiguration((hostingContext, config) => + //{ + // config.AddJsonFile("appsettings.json", false, true); + //}) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/DockerStudyApi/DockerStudyApi/Startup.cs b/DockerStudyApi/DockerStudyApi/Startup.cs index df237c6..1839471 100644 --- a/DockerStudyApi/DockerStudyApi/Startup.cs +++ b/DockerStudyApi/DockerStudyApi/Startup.cs @@ -1,3 +1,10 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Reflection; + using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; @@ -7,17 +14,14 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Schema; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; + + namespace DockerStudyApi { public class Startup @@ -49,7 +53,13 @@ namespace DockerStudyApi services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "DockerStudyApi", Version = "v1" }); - }); + + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); + c.IncludeXmlComments(xmlPath,true); + + }).AddSwaggerGenNewtonsoftSupport(); + } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -58,10 +68,19 @@ namespace DockerStudyApi if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - app.UseSwagger(); - app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "DockerStudyApi v1")); } + app.UseSwagger(setup => + { + + }); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "DockerStudyApi v1"); + c.EnableFilter(); + //c.ShowCommonExtensions(); + }); + app.UseRouting(); app.UseAuthorization(); diff --git a/DockerStudyApi/DockerStudyApi/true b/DockerStudyApi/DockerStudyApi/true new file mode 100644 index 0000000..ee1a49a --- /dev/null +++ b/DockerStudyApi/DockerStudyApi/true @@ -0,0 +1,32 @@ + + + + DockerStudyApi + + + + + 配置测试控制器 + + + + + 服务测试 + + + + + + + + + 获取环境变量 + + + + + 获取所有配置项 + + + +