• windows服务autofac注入quartz任务


    一、nuget下载相关类库引用

      install-package Quartz

          install-package Autofac

      install-package Autofac.Configuration

          install-package Autofac.Extras.Quartz

    二、配置autofac注入

    public class AutofacUtil
        {
            /// <summary>
            /// Autofac容器对象
            /// </summary>
            private static IContainer _container;
    
            /// <summary>
            /// 初始化autofac
            /// </summary>
            public static void InitAutofac()
            {
                var builder = new ContainerBuilder();
    
                //配置接口依赖
                builder.RegisterInstance<IDbConnection>(DBFactory.CreateConnection()).As<IDbConnection>();
                builder.RegisterGeneric(typeof(GenericRepository<>)).As(typeof(IGenericRepository<>));
    
                //注入仓储类
                builder.RegisterAssemblyTypes(Assembly.Load("Demo.Repository"))
                       .Where(x => x.Name.EndsWith("Repository"))
                       .AsImplementedInterfaces();
    
                //配置quartz.net依赖注入
                builder.RegisterModule(new QuartzAutofacFactoryModule());
                builder.RegisterModule(new QuartzAutofacJobsModule(Assembly.GetExecutingAssembly()));
    
                _container = builder.Build();
            }
    
            /// <summary>
            /// 从Autofac容器获取对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static T GetFromFac<T>()
            {
                return _container.Resolve<T>();
            }
        }

    三、windows服务注入autofac

        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            static void Main()
            {
                //windows服务初始化工作
                AutofacUtil.InitAutofac();
    
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                ServiceBase.Run(ServicesToRun);
            }
        }

    四、quartz任务中注入服务

     /// <summary>
        /// 推送短信任务
        /// </summary>
        public class PushSMSJob : IJob
        {
            private readonly ISMSNoticeRepository _smsNoticeRepository;
    
            public PushSMSJob(ISMSNoticeRepository smsNoticeRepository)
            {
                _smsNoticeRepository = smsNoticeRepository;
            }
    
            /// <summary>
            /// 执行推送短信的具体实现代码
            /// </summary>
            /// <param name="context"></param>
            public void Execute(IJobExecutionContext context)
            {
                //获取待发送短信列表
                var waitSendList = _smsNoticeRepository.GetWaitForSendSMS();
            }
        }
  • 相关阅读:
    一种可以实时检测IP地址合法性的EditText输入框
    LVDS 屏幕 M215HGE-L21 在 rk3288 上的适配过程
    轻读一下 Android 应用开发中的 assets 目录
    XML与其在Android下的解析
    Linux Shell脚本实现根据进程名杀死进程
    RSA host key has changed 错误
    Linux下安装jdk8步骤详述
    Windows/Linux javac/java编译运行引入所需的jar包
    No cached version of ..... available for offline mode.
    Java学习之InputStream中read()与read(byte[] b)
  • 原文地址:https://www.cnblogs.com/htsboke/p/10980779.html
Copyright © 2020-2023  润新知