Hello everyone!
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.
at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 parameters) at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType) at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context) at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0() at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func
1 continuation)
at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)
at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)