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
();
workManager.Add(IocManager.Resolve(DeleteOldAuditLogsWorker());
}
}
复制代码
(4).添加DeleteOldAuditLogsWorker类
复制代码
public class DeleteOldAuditLogsWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
{
private readonly IRepository _auditLogRepository;
public DeleteOldAuditLogsWorker(AbpTimer timer,IRepository 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类中,添加如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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(
// Configure Log4Net logging
options => options.IocManager.IocContainer.AddFacility(
f => f.UseAbpLog4Net().WithConfig("log4net.config")
);
options.PlugInSources.AddFolder(Path.Combine(_hostingEnvironment.WebRootPath, "Plugins"), SearchOption.AllDirectories);
);
}
...
}
(3)运行程序,查看Logs.txt日志记录https://www.cnblogs.com/OlderGiser/p/10015246.html