lzyq88娱乐lzyq88娱乐


lzyq88游戏

【ABP框架系列学习】模块系统(4)之示例开发

0.引言

上一篇博文主要介绍了ABP模块及插件的相关知识,本章节主要开发一个插件示例来学习如何创建一个插件,并在应用程序中使用。这个命名为FirstABPPlugin的插件主要在指定的时间段内删除审计日志。

1.创建插件

(1).新建项目,选择【类库(.NET Core)】

(2).添加引用Abp、Abp.ZeroCore

(3).创建FirstABPPluginModule类,继承AbpModule类和声明依赖于AbpZeroCoreModule

[DependsOn(typeof(AbpZeroCoreModule))] public class FirstABPPluginModule:AbpModule { public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); } public override void PostInitialize() { var workManager = IocManager.Resolve<IBackgroundWorkerManager>(); workManager.Add(IocManager.Resolve(DeleteOldAuditLogsWorker()); } }

(4).添加DeleteOldAuditLogsWorker类

public class DeleteOldAuditLogsWorker : PeriodicBackgroundWorkerBase, ISingletonDependency { private readonly IRepository<AuditLog, long> _auditLogRepository; public DeleteOldAuditLogsWorker(AbpTimer timer,IRepository<AuditLog,long> auditLogRepository) : base(timer) { _auditLogRepository = auditLogRepository; Timer.Period = 5000; } [UnitOfWork] protected override void DoWork() { Logger.Info("---------------- DeleteOldAuditLogsWorker 正在工作 ----------------"); using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant)) { var fiveMinutesAgo = Clock.Now.Subtract(TimeSpan.FromMinutes(5)); _auditLogRepository.Delete(log => log.ExecutionTime > fiveMinutesAgo); CurrentUnitOfWork.SaveChanges(); } } }

(5).最终结构如下

(6).生成项目,在bin/Debug/netcoreapp2.1目录下生成FirstABPPlugin.dll

2.添加插件到应用程序

(1).启动ABP项目模版生成的程序,把刚生成的FirstABPPlugin.dll拷贝到wwwroot/Plugins目录下

(2).在Mvc项目的Startup.cs类中,添加如下代码:

public class Startup{ private readonly IConfigurationRoot _appConfiguration; public Startup(IHostingEnvironment env) { _appConfiguration = env.GetAppConfiguration(); } public IServiceProvider ConfigureServices(IServiceCollection services) { ... // Configure Abp and Dependency Injection return services.AddAbp<AbpProjectNameWebMvcModule>( // Configure Log4Net logging options => options.IocManager.IocContainer.AddFacility<LoggingFacility>( f => f.UseAbpLog4Net().WithConfig("log4net.config") ); options.PlugInSources.AddFolder(Path.Combine(_hostingEnvironment.WebRootPath, "Plugins"), SearchOption.AllDirectories); ); } ...}

(3)运行程序,查看Logs.txt日志记录

欢迎阅读本文章: 李成青

lzyq88官网

lzyq88游戏