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