.NET?Core使用APB?vNext框架入門教程
快速入門案例
.NET Core 控制臺(tái)應(yīng)用
1. 安裝 ABP 框架核心依賴
Install-Package Volo.Abp.Core -Version 3.3.2
2. 新建 ABP 應(yīng)用的啟動(dòng)模塊
HelloAbpModule.cs
using Volo.Abp.Modularity;
namespace HelloAbp
{
/// <summary>
/// 啟動(dòng)模塊
/// </summary>
public class HelloAbpModule : AbpModule
{
}
}3. 新建服務(wù),并注冊(cè)到啟動(dòng)模塊中
HelloWorldService.cs
using System;
using Volo.Abp.DependencyInjection;
namespace HelloAbp
{
/// <summary>
/// TODO: ABP 注冊(cè)服務(wù)方式一: 繼承接口
/// ISingletonDependency、IScopedDependency、ITransientDependency
/// </summary>
public class HelloWorldService : ITransientDependency
{
public void Run()
{
Console.WriteLine($"{nameof(HelloAbpModule)}-{nameof(HelloWorldService)}: Hello World!");
}
}
}4. 根據(jù)啟動(dòng)模塊創(chuàng)建 ABP應(yīng)用,調(diào)用應(yīng)用中注冊(cè)的服務(wù)方法
Program.cs
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
namespace HelloAbp
{
class Program
{
static void Main(string[] args)
{
// 根據(jù)啟動(dòng)模塊創(chuàng)建 abp 應(yīng)用
var application = AbpApplicationFactory.Create<HelloAbpModule>();
// 初始化 abp 應(yīng)用
application.Initialize();
// 獲取應(yīng)用中注冊(cè)的服務(wù)
var service = application.ServiceProvider.GetService<HelloWorldService>();
// 調(diào)用應(yīng)用中的服務(wù)方法
service.Run();
Console.ReadKey();
}
}
}ASP.NET Core Web 應(yīng)用程序
1. 安裝 ABP 框架核心依賴
Install-Package Volo.Abp.Core -Version 3.3.2 Install-Package Volo.Abp.AspNetCore.Mvc -Version 3.3.2
2.新建 ABP 應(yīng)用的啟動(dòng)模塊
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>
/// 啟動(dòng)模塊
/// TODO: 1.在啟動(dòng)模塊中配置 ASP.NET Core Web 程序的管道,就需要定義對(duì) ASP.NET Core Mvc模塊的依賴
/// </summary>
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
public class AppModule : AbpModule
{
/// <summary>
/// 應(yīng)用初始化方法
/// TODO: 2.重寫 ABP 應(yīng)用的初始化方法,用來(lái)構(gòu)建 ASP.NET Core 應(yīng)用程序的中間件管道
/// </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.將 程序應(yīng)用的端點(diǎn)配置 修改為 ABP 應(yīng)用的端點(diǎn)配置
app.UseConfiguredEndpoints();
}
}
}3. 注冊(cè) ABP 啟動(dòng)模塊,并初始化 ABP 應(yīng)用
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>
/// 程序啟動(dòng)類
/// TODO: 3. 在 Startup 類中,完成對(duì) ABP 應(yīng)用的初始化
/// </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();
}
}
}4. 新建控制器,測(cè)試 ABP 應(yīng)用運(yùn)行狀態(tài)
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 框架中的基類控制器(提供了一些便捷的服務(wù)和方法)
/// </summary>
public class HomeController : AbpController
{
public IActionResult Index()
{
return Content("Hello world!");
}
}
}各個(gè)擊破案例
ABP應(yīng)用中的模塊可以有很多個(gè),但是啟動(dòng)模塊只能有一個(gè);
ABP應(yīng)用中的每個(gè)模塊之間沒(méi)有必然的聯(lián)系;
ABP應(yīng)用中每個(gè)模塊注冊(cè)的服務(wù),都注冊(cè)到了ABP應(yīng)用的全局容器中;
ABP應(yīng)用中的模塊也分為兩種類型:應(yīng)用程序模塊(業(yè)務(wù)實(shí)現(xiàn))和框架模塊(技術(shù)實(shí)現(xiàn));
ABP應(yīng)用中最頂層的模塊是啟動(dòng)模塊,最后被加載的也是啟動(dòng)模塊。
在模塊中注冊(cè)自定義服務(wù)
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>
/// 啟動(dòng)模塊
/// </summary>
public class HelloAbpModule : AbpModule
{
// TODO: 重寫 ABP 模塊的服務(wù)配置方法,向模塊中注冊(cè)自定義的服務(wù)
public override void ConfigureServices(ServiceConfigurationContext context)
{
base.ConfigureServices(context);
// TODO: ABP 注冊(cè)服務(wù)方式二: 模塊注冊(cè)
context.Services.AddTransient<HelloWorldService>();
}
}
}小結(jié)
初始化ABP模塊
- 1.注冊(cè)ABP基礎(chǔ)設(shè)施與核心服務(wù)(模塊系統(tǒng)相關(guān))
- 2.加載整個(gè)應(yīng)用的所有模塊,按照依賴性排序
- 3.按順序遍歷所有模塊,執(zhí)行每一個(gè)模塊的配置方法
- 4.按順序遍歷所有模塊,執(zhí)行每一個(gè)模塊的初始化方法
使用標(biāo)簽屬性注冊(cè)自定義服務(wù)
HelloWorldService.cs
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
namespace HelloAbp
{
/// <summary>
/// TODO: ABP 注冊(cè)服務(wù)方式三: 特性標(biāo)簽
/// ServiceLifetime.Singleton、ServiceLifetime.Scoped、ServiceLifetime.Transient
/// </summary>
[Dependency(ServiceLifetime.Transient)]
public class HelloWorldService
{
public void Run()
{
Console.WriteLine($"{nameof(HelloAbpModule)}-{nameof(HelloWorldService)}: Hello World!");
}
}
}ABP 項(xiàng)目中使用 Autofac
1. 安裝 Autofac 模塊
Install-Package Volo.Abp.Autofac -Version 3.3.2
2. 在模塊中創(chuàng)建對(duì) Autofac 模塊的依賴
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>
/// 啟動(dòng)模塊
/// </summary>
// TODO: 使用 Autofac 第三方依賴注入框架(提供了更多的高級(jí)特性)
[DependsOn(typeof(AbpAutofacModule))]
public class HelloAbpModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
base.ConfigureServices(context);
context.Services.AddTransient<HelloWorldService>();
}
}
}3. 在ABP應(yīng)用創(chuàng)建時(shí)集成 Autofac
Program.cs
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
namespace HelloAbp
{
class Program
{
static void Main(string[] args)
{
{
// 根據(jù)啟動(dòng)模塊創(chuàng)建 abp 應(yīng)用
var application = AbpApplicationFactory.Create<HelloAbpModule>(options =>
{
// 集成 Autofac
options.UseAutofac();
});
// 初始化 abp 應(yīng)用
application.Initialize();
// 獲取應(yīng)用中注冊(cè)的服務(wù)
var service = application.ServiceProvider.GetService<HelloWorldService>();
// 調(diào)用應(yīng)用中的服務(wù)方法
service.Run();
}
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}完整案例代碼
到此這篇關(guān)于.NET Core使用APB vNext框架入門教程的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- .Net?Core3.0?WebApi?項(xiàng)目框架搭建之使用Serilog替換掉Log4j
- .Net?Core微服務(wù)rpc框架GRPC通信實(shí)際運(yùn)用
- .Net?Core微服務(wù)rpc框架GRPC通信基礎(chǔ)
- .Net使用分表分庫(kù)框架ShardingCore實(shí)現(xiàn)多字段分片
- ASP.NET Core 依賴注入框架的使用
- ASP.NET Core 3框架揭秘之 異步線程無(wú)法使用IServiceProvider問(wèn)題
- .netcore 使用surging框架發(fā)布到docker
- 詳解ASP.NET Core 中的框架級(jí)依賴注入
- ASP.NET Core應(yīng)用中與第三方IoC/DI框架的整合
相關(guān)文章
.NET的動(dòng)態(tài)編譯與WS服務(wù)調(diào)用詳解
這篇文章介紹了.NET的動(dòng)態(tài)編譯與WS服務(wù)調(diào)用詳解,有需要的朋友可以參考一下,希望對(duì)你有所幫助2013-07-07
asp.net core3.1 引用的元包dll版本兼容性問(wèn)題解決方案
這篇文章主要介紹了asp.net core 3.1 引用的元包dll版本兼容性問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
詳解開(kāi)源免費(fèi)且穩(wěn)定實(shí)用的.NET PDF打印組件itextSharp(.NET組件介紹之八)
本篇文章主要介紹了.NET PDF打印組件itextSharp。.NET中實(shí)現(xiàn)PDF打印的組件比較多,例如PDFsharp、Report.NET、sharpPDF、itextSharp等等,今天主要簡(jiǎn)單的介紹itextSharp組件。有興趣的可以了解一下。2016-12-12
基于?.NET?6?的ASP.NET?Core啟動(dòng)地址配置方法及優(yōu)先級(jí)順序
這篇文章主要介紹了ASP.NET?Core啟動(dòng)地址配置方法及優(yōu)先級(jí)順序,?.NET?6?使用了最小?WEB?API,?配置方式已經(jīng)部分發(fā)生了變化,下面文章我們來(lái)看看具體的方法,需要的小伙伴可以參考一下2022-03-03
ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間示例詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Visual Studio 2017新版發(fā)布 更強(qiáng)大!
Visual Studio 2017新版發(fā)布 更強(qiáng)大!對(duì)Visual Studio 2017感興趣的小伙伴們可以參考一下2017-05-05
幾種判斷asp.net中session過(guò)期方法的比較
幾種判斷asp.net中session過(guò)期方法的比較,需要的朋友可以參考一下2013-04-04
ASP.NET2.0緩存(Cache)技術(shù)深入理解
緩存技術(shù)是ASP.NET2.0非常重要的一個(gè)特性,它提供了一種非常好的本地?cái)?shù)據(jù)緩存機(jī)制,從而有效的提高數(shù)據(jù)訪問(wèn)的性能2012-11-11
.net 單點(diǎn)登錄的設(shè)計(jì)與實(shí)踐
本篇文章主要介紹了解析.net 單點(diǎn)登錄實(shí)踐,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11

