zoukankan      html  css  js  c++  java
  • 【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日志记录


  • 相关阅读:
    SQL Server 2008 安装过程中遇到“性能计数器注册表配置单元一致性”检查失败 问题的解决方法
    备份还原工具—ghost
    太多的if,太多的痛苦
    C#中使用GUID
    WinForm开发中,将Excel文件导入到DataGridView中时,获取Excel中所有表格的名称。
    使用ASP调用C#写的COM+组件
    COM+ and the .NET Framework 虽是英文但比较全面
    在C#中使用COM+实现事务控制
    COM+ and the .NET Framework
    管理员ID过期,无人能够管理Domino服务器
  • 原文地址:https://www.cnblogs.com/OlderGiser/p/10015246.html
Copyright © 2011-2022 走看看