abp 使用动态api
动态api:可以把 application写的服务自动转换为api接口;
项目结构图
操作步骤:
1 新增 Application.Contracts 项目
添加 Volo.abp.core 包 然后创建 Module 类 ,然后新增 ITestService 接口
Module 类如下:
using Volo.Abp.Modularity; namespace _20220605AbpDynamicApi.Application.Contracts { public class _20220605AbpDynamicApiApplicationContractsModule : AbpModule { } }
ITestService 如下:
using Volo.Abp; using Volo.Abp.DependencyInjection; namespace _20220605AbpDynamicApi.Application.Contracts { // IScopedDependency -- ioc 自动注入 // IRemoteService -- 实现动态api需要实现接口 public interface ITestService : IScopedDependency,IRemoteService { string GetPing(); } }
2 新增 Application 项目
添加 Volo.abp.core 包 然后创建 Module 类,然后创建 TestService 类实现 ITestService 接口
Module 类如下:
using _20220605AbpDynamicApi.Application.Contracts; using Volo.Abp.Modularity; namespace _20220605AbpDynamicApi.Application { [DependsOn(typeof(_20220605AbpDynamicApiApplicationContractsModule))] public class _20220605AbpDynamicApiApplicationModule : AbpModule { } }
TestService 类如下:
using _20220605AbpDynamicApi.Application.Contracts; namespace _20220605AbpDynamicApi.Application { public class TestService : ITestService { public string GetPing() { return "Pong"; } } }
3 新增 WebApi 项目
添加 Volo.abp.core Volo.Abp.AspNetCore.Mvc 包,然后创建 Module 类,修改 Startup.cs 使用 Module 类替换 asp.net core 的执行管道
Module 类如下:
using _20220605AbpDynamicApi.Application; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Modularity; namespace _20220605AbpDynamicApi { // 通过 DependsOn 来进行 ioc 注册 [DependsOn(typeof(AbpAspNetCoreMvcModule))] [DependsOn(typeof(_20220605AbpDynamicApiApplicationModule))] public class _20220605AbpDynamicApiModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { var services = context.Services; services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication_AbpAutoApi", Version = "v1" }); // 加上这两句,不然发现不了动态 api c.DocInclusionPredicate((docName, description) => true); c.CustomSchemaIds(t => t.FullName); }); // 配置动态api Configure<AbpAspNetCoreMvcOptions>(options => { // _20220605AbpDynamicApiApplicationModule 是 Application 的 Module options.ConventionalControllers.Create(typeof(_20220605AbpDynamicApiApplicationModule).Assembly); }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication_AbpAutoApi v1")); } app.UseRouting(); app.UseAuthorization(); // 加上这个不然swagger请求一直报 404 app.UseConfiguredEndpoints(); } } }
Startup.cs 类如下:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.OpenApi.Models; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace _20220605AbpDynamicApi { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddApplication<_20220605AbpDynamicApiModule>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.InitializeApplication(); } } }
4 测试
设置 web api 项目为启动项
运行结果如下: