关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回

.NET6MinimalAPIs开发实践

发布时间:2022-07-27 13:54:11

为什么使用Minimal APIs?它和带Controller的APIs有啥区别?

区别看这里:https://docs.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0&tabs=visual-studio#differences-between-minimal-apis-and-apis-with-controllers.NET6MinimalAPIs
开发实践

No support for filters: For example, no support for IAsyncAuthorizationFilter, IAsyncActionFilter, IAsyncExceptionFilter, IAsyncResultFilter, and IAsyncResourceFilter.
No support for model binding, i.e. IModelBinderProvider, IModelBinder. Support can be added with a custom binding shim.
No support for binding from forms. This includes binding IFormFile. We plan to add support for IFormFile in the future.
No built-in support for validation, i.e. IModelValidator
No support for application parts or the application model. There's no way to apply or build your own conventions.
No built-in view rendering support. We recommend using Razor Pages for rendering views.
No support for JsonPatch
No support for OData
No support for ApiVersioning. See this issue for more details.

部署为Windows Service

1. 依赖包安装

Install-Package Microsoft.Extensions.Hosting.WindowsServices

2. 使用

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseWindowsService(); //add this line
var app = builder.Build();

3. 发布项目

发布从不同维度划分分为2种
a. 是否为单文件
b. 是否Self-Contained

所以组合下来就是:
a. 基于Framework的多文件
b. 基于Framework的单文件
c. 带运行时的多文件
d. 带运行时的单文件
https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md#user-experience

命令如下:

dotnet publish -r win-x64 /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true

4. 注册服务

PowerShell

//https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-service?view=powershell-7.2
New-Service -Name {服务名} -BinaryPathName "{exe文件路径}" -Description "{描述}" -DisplayName "{显示名}" -StartupType Automatic

或者使用sc.exe

sc <server> create [service name] [binPath= ] <option1> <option2>...

OPTIONS:
NOTE: The option name includes the equal sign.
      A space is required between the equal sign and the value.
 type= <own|share|interact|kernel|filesys|rec|userown|usershare>
       (default = own)
 start= <boot|system|auto|demand|disabled|delayed-auto>
       (default = demand)
 error= <normal|severe|critical|ignore>
       (default = normal)
 binPath= <BinaryPathName to the .exe file>
 group= <LoadOrderGroup>
 tag= <yes|no>
 depend= <Dependencies(separated by / (forward slash))>
 obj= <AccountName|ObjectName>
       (default = LocalSystem)
 DisplayName= <display name>
 password= <password>

5. 启动服务

PowerShell

Start-Service ServiceName

不出意外的话会报错

Start-Service : Service 'Service1 (Service1)' cannot be started due to the following error: Cannot start service Service1 on computer '.'.

解决该问题有2个方案,后面官方修复了就不用这么麻烦了
a. 改代码

using Microsoft.Extensions.Hosting.WindowsServices;

var options = new WebApplicationOptions
{
    Args = args,
    ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

builder.Host.UseWindowsService();

b. 上面代码可以看出来,其实就是Windows Service需要的ContentRootPath有问题
那么我们可以在启动服务的时候指定--contentRoot参数

sc config MyWebAppServiceTest binPath= "$pwdWebApplication.exe --contentRoot $pwd"

本段参考:
https://stackoverflow.com/questions/69124310/asp-net-core-net-6-preview-7-windows-service
https://github.com/dotnet/AspNetCore.Docs/issues/23387


/template/Home/DawnNew/PC/Static

立即注册风纳云账号,免费体验多款产品

立即注册