这篇文章给大家分享的是有关.NET Core如何使用APB vNext框架的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
Install-Package Volo.Abp.Core -Version 3.3.2
HelloAbpModule.cs
using Volo.Abp.Modularity;
namespace HelloAbp
{
/// <summary>
/// 启动模块
/// </summary>
public class HelloAbpModule : AbpModule
{
}
}
HelloWorldService.cs
using System;
using Volo.Abp.DependencyInjection;
namespace HelloAbp
{
/// <summary>
/// TODO: ABP 注册服务方式一: 继承接口
/// ISingletonDependency、IScopedDependency、ITransientDependency
/// </summary>
public class HelloWorldService : ITransientDependency
{
public void Run()
{
Console.WriteLine($"{nameof(HelloAbpModule)}-{nameof(HelloWorldService)}: Hello World!");
}
}
}
Program.cs
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
namespace HelloAbp
{
class Program
{
static void Main(string[] args)
{
// 根据启动模块创建 abp 应用
var application = AbpApplicationFactory.Create<HelloAbpModule>();
// 初始化 abp 应用
application.Initialize();
// 获取应用中注册的服务
var service = application.ServiceProvider.GetService<HelloWorldService>();
// 调用应用中的服务方法
service.Run();
Console.ReadKey();
}
}
}
Install-Package Volo.Abp.Core -Version 3.3.2 Install-Package Volo.Abp.AspNetCore.Mvc -Version 3.3.2
AppModule.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;
namespace HelloWebAbp
{
/// <summary>
/// 启动模块
/// TODO: 1.在启动模块中配置 ASP.NET Core Web 程序的管道,就需要定义对 ASP.NET Core Mvc模块的依赖
/// </summary>
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
public class AppModule : AbpModule
{
/// <summary>
/// 应用初始化方法
/// TODO: 2.重写 ABP 应用的初始化方法,用来构建 ASP.NET Core 应用程序的中间件管道
/// </summary>
/// <param name="context"></param>
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
// base.OnApplicationInitialization(context);
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
// TODO: 5.将 程序应用的端点配置 修改为 ABP 应用的端点配置
app.UseConfiguredEndpoints();
}
}
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace HelloWebAbp
{
/// <summary>
/// 程序启动类
/// TODO: 3. 在 Startup 类中,完成对 ABP 应用的初始化
/// </summary>
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddApplication<AppModule>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.InitializeApplication();
}
}
}
HomeController.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
namespace HelloWebAbp.Controllers
{
/// <summary>
/// 控制器
/// TODO: 4. 继承 Abp 框架中的基类控制器(提供了一些便捷的服务和方法)
/// </summary>
public class HomeController : AbpController
{
public IActionResult Index()
{
return Content("Hello world!");
}
}
}
ABP应用中的模块可以有很多个,但是启动模块只能有一个;
ABP应用中的每个模块之间没有必然的联系;
ABP应用中每个模块注册的服务,都注册到了ABP应用的全局容器中;
ABP应用中的模块也分为两种类型:应用程序模块(业务实现)和框架模块(技术实现);
ABP应用中最顶层的模块是启动模块,最后被加载的也是启动模块。
HelloAbpModule.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Modularity;
namespace HelloAbp
{
/// <summary>
/// 启动模块
/// </summary>
public class HelloAbpModule : AbpModule
{
// TODO: 重写 ABP 模块的服务配置方法,向模块中注册自定义的服务
public override void ConfigureServices(ServiceConfigurationContext context)
{
base.ConfigureServices(context);
// TODO: ABP 注册服务方式二: 模块注册
context.Services.AddTransient<HelloWorldService>();
}
}
}
小结
初始化ABP模块
1.注册ABP基础设施与核心服务(模块系统相关)
2.加载整个应用的所有模块,按照依赖性排序
3.按顺序遍历所有模块,执行每一个模块的配置方法
4.按顺序遍历所有模块,执行每一个模块的初始化方法
HelloWorldService.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
namespace HelloAbp
{
/// <summary>
/// TODO: ABP 注册服务方式三: 特性标签
/// ServiceLifetime.Singleton、ServiceLifetime.Scoped、ServiceLifetime.Transient
/// </summary>
[Dependency(ServiceLifetime.Transient)]
public class HelloWorldService
{
public void Run()
{
Console.WriteLine($"{nameof(HelloAbpModule)}-{nameof(HelloWorldService)}: Hello World!");
}
}
}
Install-Package Volo.Abp.Autofac -Version 3.3.2
HelloAbpModule.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace HelloAbp
{
/// <summary>
/// 启动模块
/// </summary>
// TODO: 使用 Autofac 第三方依赖注入框架(提供了更多的高级特性)
[DependsOn(typeof(AbpAutofacModule))]
public class HelloAbpModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
base.ConfigureServices(context);
context.Services.AddTransient<HelloWorldService>();
}
}
}
Program.cs
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
namespace HelloAbp
{
class Program
{
static void Main(string[] args)
{
{
// 根据启动模块创建 abp 应用
var application = AbpApplicationFactory.Create<HelloAbpModule>(options =>
{
// 集成 Autofac
options.UseAutofac();
});
// 初始化 abp 应用
application.Initialize();
// 获取应用中注册的服务
var service = application.ServiceProvider.GetService<HelloWorldService>();
// 调用应用中的服务方法
service.Run();
}
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
感谢各位的阅读!关于“.NET Core如何使用APB vNext框架”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。