一、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();
}
}

最新文章

  1. Card Flip
  2. 2. javacript高级程序设计-在HTML中使用JavaScript
  3. 夺命雷公狗---node.js---7fs模块初步
  4. SQL Server Native Client 安装方法
  5. eclipse中文乱码
  6. POJ1502(Dijkstra)
  7. eclipse扩容
  8. Nginx配置文件(2)
  9. 常用的String原型
  10. OKR源自德鲁克和格鲁夫,跟谷歌是天作之合:4星|《这就是OKR》
  11. input表单提交完毕,返回重新填入有黄色背景,和 历史记录 清除
  12. 关于Shader的学习记录
  13. LDA算法学习(Matlab实现)
  14. sed匹配多行替换
  15. 反射简介—C#特性和反射
  16. spark-submit 提交Application
  17. CentOS里vim基本操作
  18. easy-animation | Animation for Sass
  19. 如何让你的项目同时支持go vendor和go module
  20. Java 获取指定包下的所有类

热门文章

  1. 01-k8s 架构
  2. Linux ln 命令
  3. 数据类型之Integer与int
  4. jQuery插件之路(一)——试着给jQuery的一个Carousel插件添加新的功能
  5. java-web调用后台下载方法
  6. tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)
  7. Unity工程无代码化
  8. MySQL储存过程详解
  9. javacv——读取摄像头的图像、截取视频的画面
  10. java中什么是继承笔记