1.需要的nuget包
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Hosting
Swashbuckle.AspNetCore
2.启停服务类
using EdgeCommon;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.Net;
using System.Threading.Tasks;
namespace EdgeCommuting
{
internal class HostHelper
{
private static IHost host = default!;
internal async Task<bool> StartAsync(HttpDefine httpDefine, string verson)
{
IPAddress? ip = default!;
if (!httpDefine.ListenAnyIp &&
!IPAddress.TryParse(httpDefine.IpAddress, out ip))
{
LocalLog.Api($"未能开启web服务:IP地址格式错误{httpDefine.IpAddress}");
return false;
}
try
{
host = Host.CreateDefaultBuilder().ConfigureWebHostDefaults(web =>
{
web.UseKestrel();
web.ConfigureKestrel((config, options) =>
{
if (httpDefine.ListenAnyIp)
{
options.ListenAnyIP(httpDefine.Port,
listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
if (httpDefine.Https.UseHttps)
listenOptions.UseHttps(httpDefine.Https.CertificateName,
httpDefine.Https.CertificatePassword);
}
);
}
else
{
options.Listen(ip, httpDefine.Port, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
if (httpDefine.Https.UseHttps)
listenOptions.UseHttps(httpDefine.Https.CertificateName,
httpDefine.Https.CertificatePassword);
});
}
options.Limits.MaxConcurrentConnections = 200;
options.Limits.MaxConcurrentUpgradedConnections = 200;
options.Limits.MaxRequestBodySize = 1000_0000;
options.Limits.MinRequestBodyDataRate =
new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
options.Limits.MinResponseDataRate =
new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
});
web.ConfigureServices(services =>
{
services.AddControllers();
if (httpDefine.UserSwagger)
{
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("Bruce", new OpenApiInfo()
{
Version = verson,
Title = "Bruce的Sweeger",
Description = "Bruce的API接口"
});
});
}
});
web.Configure(app =>
{
app.UseSwagger();
app.UseSwaggerUI(option =>
{
option.SwaggerEndpoint($"/swagger/Bruce/swagger.json", "Bruce");
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
}).Build();
await host.StartAsync();
LocalLog.Api("web服务已启动");
return true;
}
catch (Exception e)
{
LocalLog.Api($"web服务启动失败:{e.Message}");
return false;
}
}
internal async Task StopAsync()
{
if (host != null)
{
using (host)
{
await host.StopAsync();
}
LocalLog.Api("web服务已关闭");
}
}
}
}
备注:using EdgeCommon关联的解决方案的其它类,无参考作用.
3. Controler
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EdgeCommuting
{
[Route("DDMData")]
[ApiController]
[ApiExplorerSettings(GroupName = "Bruce")]
public class MesController : ControllerBase
{
[HttpPost("Config")]
public ApiResponse Config([FromBody] PCConfig config)
{
//只是示例,根据需要编写代码
return new();
}
[HttpPost("Param")]
public ApiResponse Param([FromBody] ApiParamReceive param)
{
//只是示例,根据需要编写代码
return new();
}
}
}
4.勾选API文档输出
项目属性->输出->生成包含API文档的文件
5.Sweeger查看效果
也可以使用Postman等工具访问接口.
本机测试浏览器输入http://localhost:8060/swagger;备注:服务监听的是8060端口