Sending recurring mail in background in every ten minutes

158 Views Asked by At

I want to send mail to admin in every ten minutes after the application has started. Simple recurring tasks has been running. But when I try to send mail, I get "Autofac.Core.Registration.ComponentNotRegisteredException". Here is what I have done.

I have created a middleware "SchedularMiddleware" and used it like in my Configure method in startup.cs

 app.UseSchedularMiddleware();

And here is the SchedularMiddleware

public class SchedularMiddleware : Controller
{
   private readonly RequestDelegate _next;

   public SchedularMiddleware(RequestDelegate next, ISendEmailService sendEmailService)
   {
      _next = next;
      sendEmailService.SendMail("[email protected]");
   }
   public async Task Invoke(HttpContext httpContext)
   {           
      await _next.Invoke(httpContext);
   }
}

And here is the implementation of Service

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

This simple task is running but when I try to send mail I get error.

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

Here is the stack trace of the error.

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

There are 0 best solutions below