【ABP框架系列學習】模塊系統(4)之示例開發
阿新 • • 發佈:2018-11-25
etc ... 類庫 logging chm sele gre 工作 繼承
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日誌記錄
【ABP框架系列學習】模塊系統(4)之示例開發