每十分钟在后台发送定期邮件

时间:2017-04-27 05:24:01

标签: c# asp.net .net hangfire hangfire-autofac

我想在应用程序启动后每隔十分钟向管理员发送邮件。简单的重复任务一直在运行。但是当我尝试发送邮件时,我得到了" Autofac.Core.Registration.ComponentNotRegisteredException"。这就是我所做的。

我创建了一个中间件" SchedularMiddleware"并像在startup.cs中的Configure方法中那样使用它

 app.UseSchedularMiddleware();

这是SchedularMiddleware

public class SchedularMiddleware : Controller
{
   private readonly RequestDelegate _next;

   public SchedularMiddleware(RequestDelegate next, ISendEmailService sendEmailService)
   {
      _next = next;
      sendEmailService.SendMail("wayne@gmail.com");
   }
   public async Task Invoke(HttpContext httpContext)
   {           
      await _next.Invoke(httpContext);
   }
}

这是Service

的实现
RecurringJob.AddOrUpdate(() => SendMail(), Cron.MinuteInterval(5));

这个简单的任务正在运行,但是当我尝试发送邮件时,我收到错误。

RecurringJob.AddOrUpdate(() => Console.WriteLine("manchester derby!"), Cron.MinuteInterval(1));

这是错误的堆栈跟踪。

Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'Demo.Module.Schedular.Services.SendEmailService' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

0 个答案:

没有答案