Sending email using hangfire as a recurring job in dot net core project

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


And here is the SchedularMiddleware

public class SchedularMiddleware : Controller
    private readonly RequestDelegate _next;

    public SchedularMiddleware(RequestDelegate next, ISendEmailService sendEmailService)
        _next = next;
    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, Func1 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)

There appear to be multiple issues here. For the purposes of answering the question, I don’t see where the code you posted matches up with the problem you’re having. I’m not seeing where you’re scheduling a job that takes a dependency on SendEmailService. Where is that code?

Also, a quibble, but in English it’s spelled “scheduler”.

ah thanks for your reply but I have got it working!

How you’ve fixed it? Got the same issue

Here is what I have done to resolve this issue. I was having problem with my services not being resolved. So, I did following to get it working:

    public static IServiceProvider Build(this IServiceCollection services,
        IConfigurationRoot configuration, IHostingEnvironment hostingEnvironment)
        var builder = new ContainerBuilder();
        builder.RegisterType<SendEmailService>().AsSelf().As<ISendEmailService >();

        builder.Register<SingleInstanceFactory>(ctx =>
            var c = ctx.Resolve<IComponentContext>();
            return t => c.Resolve(t);

        builder.Register<MultiInstanceFactory>(ctx =>
            var c = ctx.Resolve<IComponentContext>();
            return t => (IEnumerable<object>)c.Resolve(typeof(IEnumerable<>).MakeGenericType(t));

        foreach (var module in Infrastructure.GlobalConfiguration.Modules)

        var container = builder.Build();
        return container.Resolve<IServiceProvider>();

Hope, it helps you!

1 Like