ApiLite是基于.NET6直接将Service层生成动态api路由,可以不用添加Controller,支持模块插件化,在项目开发中能够提高工作效率,降低代码量。
主要是ApiFeatureProvider和ApiConvention这两个自定义类来动态生成api,ApiFeatureProvider继承ControllerFeatureProvider,覆写IsController方法,判断服务类型是否符合Controller。ApiConvention实现了IApplicationModelConvention接口,实现动态添加Action。下面是主要代码,完整代码请在GitHub上下载。
static class ServiceExtension { internal static WebApplicationBuilder AddKApp(this WebApplicationBuilder builder, Action<AppOption>? action = null) { var option = new AppOption(); action?.Invoke(option); ... AddDynamicApi(mvcBuilder, option);//添加动态api return builder; } private static void AddDynamicApi(IMvcBuilder builder, AppOption option) { builder.ConfigureApplicationPartManager(m => { m.ApplicationParts.Add(new AssemblyPart(typeof(IService).Assembly)); foreach (var item in option.Modules) { item.Initialize();//初始化模块 //将模块添加到ApplicationParts,这样才能发现服务类 var assembly = item.GetType().Assembly; m.ApplicationParts.Add(new AssemblyPart(assembly)); } m.FeatureProviders.Add(new ApiFeatureProvider()); }); builder.Services.Configure<MvcOptions>(o => { o.Conventions.Add(new ApiConvention()); }); } } //判断服务类型是否为Controller class ApiFeatureProvider : ControllerFeatureProvider { protected override bool IsController(TypeInfo typeInfo) { if (!typeof(IService).IsAssignableFrom(typeInfo) || !typeInfo.IsPublic || typeInfo.IsAbstract || typeInfo.IsGenericType) return false; return true; } } class ApiConvention : IApplicationModelConvention { public void Apply(ApplicationModel application) { foreach (var controller in application.Controllers) { var type = controller.ControllerType; if (typeof(IService).IsAssignableFrom(type)) { ConfigureApiExplorer(controller); ConfigureSelector(controller); } } } ... //构造路由模板 private string GetRouteTemplate(ActionModel action) { if (action.Attributes != null && action.Attributes.Count > 0) { foreach (var item in action.Attributes) { if (item is RouteAttribute attribute) { return attribute.Path;//返回自定义路由 } } } var routeTemplate = new StringBuilder(); //routeTemplate.Append("api"); var names = action.Controller.ControllerType.Namespace.Split('.'); if (names.Length > 2) { //支持不同模块相同类名,添加命名空间模块名作前缀 routeTemplate.Append(names[^2]); } // Controller var controllerName = action.Controller.ControllerName; if (controllerName.EndsWith("Service")) controllerName = controllerName[0..^7]; routeTemplate.Append($"/{controllerName}"); // Action var actionName = action.ActionName; if (actionName.EndsWith("Async")) actionName = actionName[..^"Async".Length]; if (!string.IsNullOrEmpty(actionName)) routeTemplate.Append($"/{actionName}"); return routeTemplate.ToString(); } }
KHost.Run(args, o => { o.Modules.Add(new TestModule());//添加模块 }); class TestModule : IModule { public void Initialize() { } } public class TestService : IService { public string GetName(string name) { return $"Hello {name}"; } public string SaveData(string data) { return $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {data}"; } [Route("api/test")] public string GetCustMethod(string id) { return id; } }
原文链接:https://www.cnblogs.com/known/p/15499542.html