1 首先创建一个WebApi项目
2 初步整合ABP模块化
Abp官网,这里我们先初步整合WebApi模块化,待程序运行起来,后续文章我们再对Abp进行深入。
2.1 NuGet:包Volo.Abp
Volo,Abp:模块化必备
2.2 NuGet:Volo.Abp.AspNetCore
Volo.Abp.AspNetCore:装配Web项目DI容器注入必备
2.3 NuGet:Volo.Autofac
Volo.Autofac:Abp的DI容器
2.4.创建一个模块化Module类
[DependsOn(typeof(AbpAspNetCoreModule))]
public class AdminGatewayModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
// Add services to the container.
context.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
context.Services.AddEndpointsApiExplorer();
context.Services.AddSwaggerGen();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var env = context.GetEnvironment();
var app = context.GetApplicationBuilder();
if (env.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.UseEndpoints(endpoint =>
{
endpoint.MapControllers();
});
}
}
2.5 修改Program启动配置
public class Program
{
public async static Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json",
optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
}).UseAutofac();
await builder.AddApplicationAsync<IdentityServerHostModule>();
var app = builder.Build();
await app.InitializeApplicationAsync();
await app.RunAsync();
}
}
注:这里有一点值得注意,在默认创建的WebApi项目中,路由映射是:app.MapControllers();,在Abp的Module中6.0版本不能直接引用,需要替换一下,否则会返回404。
3 替换并启动
替换并启动项目后,一切正常运行!