0

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("wayne@gmail.com");
   }
   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.
Meer
  • 2,765
  • 2
  • 19
  • 28
user3127109
  • 3,421
  • 8
  • 24
  • 33
  • 1
    have you register `ISendEmailService `service with autofac? this error seems to tell `ISendEmailService ` is not registered with autofac – Curiousdev Apr 27 '17 at 07:01
  • I have registered these services through IServiceProvider – user3127109 Apr 27 '17 at 07:18
  • You're using `asp.net core` right? add `builder.RegisterType().As();` In the ConfigureServices method of your Startup class or anywhere else where you are registering your other dependencies if it does not resolve your problem show us your startup.cs class – Curiousdev Apr 27 '17 at 07:25
  • Yeah I am using dot net core! Here is what I have done. I have static class like IServiceCollection AddFrameworkServices(this IServiceCollection services, IConfigurationRoot configuration){} where I resgister my services like services.Add(). Then in my ConfigureServices of Startup.cs, I include it like services.AddFrameworkServices(Configuration); – user3127109 Apr 27 '17 at 07:29
  • Ok that i can understand but did you put `builder.RegisterType().As();` this? if you want to know more about dependency registries using autofac read [this](http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services) document as well – Curiousdev Apr 27 '17 at 09:36

0 Answers0