189 8069 5689

.NET6MinimalAPIs开发实践-创新互联

为什么使用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  create [service name] [binPath= ]  ...

OPTIONS:
NOTE: The option name includes the equal sign.
      A space is required between the equal sign and the value.
 type= 
       (default = own)
 start= 
       (default = demand)
 error= 
       (default = normal)
 binPath= 
 group= 
 tag= 
 depend= 
 obj= 
       (default = LocalSystem)
 DisplayName= 
 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


本文题目:.NET6MinimalAPIs开发实践-创新互联
本文URL:http://cdxtjz.cn/article/cogdid.html

其他资讯