From fad9428656bf49a46d94be07355f8eb3ba4b9ad0 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 14 Oct 2024 14:48:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5minimal=20api=20=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/Progranm.cs | 33 +++++++++++++- .../Swagger/SwaggerSetup.cs | 3 ++ .../IRaCIS.Core.Application.csproj | 2 + .../IRaCIS.Core.Application.xml | 5 +++ IRaCIS.Core.Application/TestService.cs | 45 ++++++++++++++++++- .../Extention/StringExtension.cs | 11 ++--- 6 files changed, 91 insertions(+), 8 deletions(-) diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index 03f60f0c3..5f3f2b534 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -1,6 +1,7 @@ using IRaCIS.Core.API; using IRaCIS.Core.Application.BusinessFilter; using IRaCIS.Core.Application.Filter; +using IRaCIS.Core.Application.MassTransit.Consumer; using IRaCIS.Core.Application.Service.BackGroundJob; using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Infrastructure.Extention; @@ -16,7 +17,9 @@ using Microsoft.Extensions.Hosting; using Newtonsoft.Json; using Serilog; using System; +using System.Collections.Generic; using System.IO; +using System.Reflection; using System.Runtime.InteropServices; AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); @@ -127,6 +130,25 @@ builder.Services.AddDicomSetup(); // 实时应用 builder.Services.AddSignalR(); +//MinimalAPI +builder.Services.AddMasaMinimalAPIs(options => +{ + options.Prefix = "";//自定义前缀 默认是api + options.Version = ""; //默认是V1 + options.AutoAppendId = false; //路由是否自动附加参数Id 默认是true + options.PluralizeServiceName = false; //服务名称是否启用复数 + + //options.Assemblies = new List() { typeof(UserSiteSurveySubmitedEventConsumer).Assembly }; + + options.GetPrefixes = new List { "Get", "Select", "Find" }; + options.PostPrefixes = new List { "Post", "Add", "Create" }; + options.PutPrefixes = new List { "Put", /*"Update"*/ }; + options.DeletePrefixes = new List { "Delete", "Remove" }; + + + options.DisableAutoMapRoute = false;//可通过配置true禁用全局自动路由映射或者删除此配置以启用全局自动路由映射 +}); + #endregion var app = builder.Build(); @@ -134,6 +156,8 @@ var env = app.Environment; #region 配置中间件 + + app.UseMiddleware(); #region 异常处理 全局业务异常已统一处理了,非业务错误会来到这里 400 -500状态码 @@ -233,6 +257,13 @@ app.MapControllers(); app.MapHub("/UploadHub"); app.MapHealthChecks("/health"); + + +#endregion + +//Map MinimalAPI routes +app.MapMasaMinimalAPIs(); + // Serilog SerilogExtension.AddSerilogSetup(enviromentName, app.Services); @@ -240,8 +271,6 @@ var hangfireJobService = app.Services.GetRequiredService(); await hangfireJobService.InitHangfireJobTaskAsync(); -#endregion - try { #region 运行环境 部署平台 diff --git a/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs index 4104c3ce1..59be986c4 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/Swagger/SwaggerSetup.cs @@ -36,6 +36,8 @@ namespace IRaCIS.Core.API options.SwaggerDoc("Image", new OpenApiInfo { Title = "影像模块", Version = "Image" }); options.SwaggerDoc("Reading", new OpenApiInfo { Title = "读片模块", Version = "Reading" }); + // Add "General" fallback group for ungrouped APIs + //options.SwaggerDoc("General", new OpenApiInfo { Title = "未分类模块", Version = "General" }); // 接口排序 options.OrderActionsBy(o => o.GroupName); @@ -47,6 +49,7 @@ namespace IRaCIS.Core.API .OfType() .Select(attr => attr.GroupName); + //return versions.Any(v => v == docName) || (docName == "General" && !versions.Any()); return versions.Any(v => v.ToString() == docName); }); diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj index 3a509ace3..eb12535aa 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.csproj @@ -59,6 +59,8 @@ + + diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 915e46add..0bd9426f9 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -12646,6 +12646,11 @@ + + + minimal api 测试 + + 清理一致性分析任务 diff --git a/IRaCIS.Core.Application/TestService.cs b/IRaCIS.Core.Application/TestService.cs index d0d5e4cde..13acd6d3e 100644 --- a/IRaCIS.Core.Application/TestService.cs +++ b/IRaCIS.Core.Application/TestService.cs @@ -14,6 +14,7 @@ using MassTransit; using MassTransit.Mediator; using Medallion.Threading; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; @@ -30,6 +31,48 @@ using Tea; namespace IRaCIS.Core.Application.Service { + /// + /// minimal api 测试 + /// + [ApiExplorerSettings(GroupName = "Institution")] + public class TestMinimalApiService : ServiceBase + { + public TestMinimalApiService() + { + RouteHandlerBuilder = t => { + t.WithGroupName("TestMinimalApi").WithOpenApi(); + }; + } + + [RoutePattern(HttpMethod = "post")] + public Task> GetProjectList1Async() + { + var list = new List() + { + "Auth", + "DCC", + "PM" + }; + return Task.FromResult(list); + } + + public List GetProjectList() + { + var list = new List() + { + "Auth", + "DCC", + "PM" + }; + return list; + } + } + + + + + + [ApiExplorerSettings(GroupName = "Institution")] public class TestService(IRepository _dicRepository, IRepository _trialRepository, @@ -126,7 +169,7 @@ namespace IRaCIS.Core.Application.Service } - + public async Task TestJson() { diff --git a/IRaCIS.Core.Infrastructure/Extention/StringExtension.cs b/IRaCIS.Core.Infrastructure/Extention/StringExtension.cs index 3c53fe507..76c7dd27d 100644 --- a/IRaCIS.Core.Infrastructure/Extention/StringExtension.cs +++ b/IRaCIS.Core.Infrastructure/Extention/StringExtension.cs @@ -11,11 +11,12 @@ namespace IRaCIS.Core.Infrastructure.Extention { - // 扩展 string 类型 - public static bool IsNullOrEmpty(this string value) - { - return string.IsNullOrEmpty(value); - } + //// 扩展 string 类型 + //public static bool IsNullOrEmpty(this string value) + //{ + // return string.IsNullOrEmpty(value); + //} + public static bool IsNotNullOrEmpty(this string value) { return !string.IsNullOrEmpty(value);