0

我想在应用程序启动后每十分钟向管理员发送一次邮件。简单的重复任务一直在运行。但是当我尝试发送邮件时,我得到“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);
   }
}

这是服务的实现

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.
4

0 回答 0