master
bicijinlian 3 years ago
parent 3beb9bfb83
commit dee082e916

@ -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"
}
]
}

42
.vscode/tasks.json vendored

@ -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"
}
]
}

@ -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
{
/// <summary>
/// 配置测试控制器
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class DemoController : ControllerBase
{
private readonly ILogger<DemoController> _logger;
private readonly IOptions<TestOption> _tespOption;
private readonly IOptionsMonitor<TestOption> _monitorTestOption;
private readonly IOptionsMonitorCache<TestOption> _monitorCacheTestOption;
private readonly IOptionsSnapshot<TestOption> _snapshotTestOption;
public DemoController(ILogger<DemoController> logger,IOptions<TestOption> tespOption)
private readonly IConfiguration _configuration;
public DemoController(ILogger<DemoController> logger, IOptions<TestOption> tespOption,IOptionsMonitor<TestOption> monitorTestOption,IOptionsMonitorCache<TestOption> monitorCacheTestOption,IOptionsSnapshot<TestOption> snapshotTestOption, IConfiguration configuration)
{
_logger = logger;
_tespOption = tespOption;
_monitorTestOption = monitorTestOption;
_monitorCacheTestOption = monitorCacheTestOption;
_snapshotTestOption = snapshotTestOption;
_configuration = configuration;
}
/// <summary>
/// 服务测试
/// </summary>
[HttpGet]
public IActionResult Test()
public IActionResult Ping()
{
return Ok();
}
/// <summary>
/// 说明泛型左右尖括号不能写在注释里会生成无效的xml
/// 解决:用[]或{}或中文符号<> 代替或者使用xml转义字符 &lt;&gt;
/// 获取 IOptionsTestOption
/// </summary>
[HttpGet]
public TestOption TestOption()
{
return _tespOption.Value;
}
/// <summary>
/// 获取 IOptionsMonitorTestOptionT
/// </summary>
[HttpGet]
public TestOption MonitorTestOption()
{
_monitorTestOption.OnChange(_ => _logger.LogWarning(_monitorTestOption.CurrentValue.AppId));
return _monitorTestOption.CurrentValue;
}
/// <summary>
/// 获取 IOptionsMonitorCacheTestOption
/// </summary>
[HttpGet]
public TestOption MonitorCacheTestOption()
{
return _monitorCacheTestOption.GetOrAdd("demo",()=> new TestOption());
}
/// <summary>
/// 获取 IOptionsSnapshotTestOption
/// </summary>
[HttpGet]
public TestOption SnapshotTestOption()
{
return _snapshotTestOption.Value;
}
/// <summary>
/// 获取环境变量
/// </summary>
[HttpGet]
public IActionResult Evn(string envName)
{
return Ok();
var envValue = _configuration.GetValue<string>(envName);
return Ok(envValue??"无此环境变量");
}
/// <summary>
/// 获取所有配置项
/// </summary>
[HttpGet]
public IActionResult Info()
{
return Ok();
var cnfigText = Newtonsoft.Json.JsonConvert.SerializeObject(_configuration.GetChildren());
return Ok(cnfigText);
}
}
}

@ -2,6 +2,8 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
@ -13,6 +15,7 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.3" />
</ItemGroup>
</Project>

@ -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

@ -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<Startup>();

@ -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();

@ -0,0 +1,32 @@
<?xml version="1.0"?>
<doc>
<assembly>
<name>DockerStudyApi</name>
</assembly>
<members>
<member name="T:DockerStudyApi.Controllers.DemoController">
<summary>
配置测试控制器
</summary>
</member>
<member name="M:DockerStudyApi.Controllers.DemoController.Ping">
<summary>
服务测试
</summary>
</member>
<!-- Badly formed XML comment ignored for member "M:DockerStudyApi.Controllers.DemoController.TestOption" -->
<!-- Badly formed XML comment ignored for member "M:DockerStudyApi.Controllers.DemoController.MonitorTestOption" -->
<!-- Badly formed XML comment ignored for member "M:DockerStudyApi.Controllers.DemoController.MonitorCacheTestOption" -->
<!-- Badly formed XML comment ignored for member "M:DockerStudyApi.Controllers.DemoController.SnapshotTestOption" -->
<member name="M:DockerStudyApi.Controllers.DemoController.Evn(System.String)">
<summary>
获取环境变量
</summary>
</member>
<member name="M:DockerStudyApi.Controllers.DemoController.Info">
<summary>
获取所有配置项
</summary>
</member>
</members>
</doc>
Loading…
Cancel
Save