时间:2022-07-19 10:18:23 | 栏目:.NET代码 | 点击:次
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(); } } }