创建模块类:
using Lemon.UserCenter.Application; using Volo.Abp.Identity; using Volo.Abp.Modularity; namespace Lemon.UserCenter.HttpApi { [DependsOn(typeof(AbpIdentityHttpApiModule), typeof(UserCenterApplicationModule))] public class UserCenterHttpApiModule : AbpModule { } }创建controller:
using System.Threading.Tasks; using Lemon.UserCenter.Application; using Lemon.UserCenter.Domain; using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc; namespace Lemon.UserCenter.HttpApi.Controllers { [Route("api/user")] public class UserController : AbpController { private readonly IUserService _userService; public UserController(IUserService userService) { this._userService = userService; } [HttpPost("create")] public async Task<IActionResult> Create(UserData data) { var result = await _userService.Create(data); return Json(result); } } } 7. 在api hosting实现项目启动项添加Volo.Abp.Autofac引用:
dotnet add Lemon.UserCenter.HttpApi.Hosting/Lemon.UserCenter.HttpApi.Hosting.csproj package Volo.Abp.Autofac创建模块类
using Lemon.UserCenter.Domain; using Lemon.UserCenter.EntityFrameworkCore; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Hosting; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Autofac; using Volo.Abp.Modularity; namespace Lemon.UserCenter.HttpApi.Hosting { [DependsOn(typeof(UserCenterHttpApiModule), typeof(UserCenterDomainModule), typeof(UserCenterentityFrameworkCoreModule), typeof(AbpAspNetCoreMvcModule), typeof(AbpAutofacModule))] public class UserCenterHttpApiHostingModule: AbpModule { public override void OnApplicationInitialization( ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseMvcWithDefaultRouteAndArea(); } } }修改Program类,新增UseAutofac:
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace Lemon.UserCenter.HttpApi.Hosting { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }).UseAutofac(); } }修改Startup类:
using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace Lemon.UserCenter.HttpApi.Hosting { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddApplication<UserCenterHttpApiHostingModule>(); } public void Configure(IApplicationBuilder app) { app.InitializeApplication(); } } } 8. 运行服务 cd Lemon.UserCenter.HttpApi.Hosting dotnet watch run 9. 最后我们用postman来测试api接口服务是否可以正常使用。操作如下图:
数据库结果如下:
以上就是接口服务的构建过程,主要参考了ABP CLI生成的项目结构,但是又有所不同。整个分层架构还可以继续优化,这个就见仁见智吧。后续还会继续分享ABP的相关知识,例如identity server 4、缓存、微服务等。
GitHub: https://github.com/huangbenq/abp-samples