Sadinus
Sadinus

Reputation: 83

How to consume scoped service with dependency in a background task

Here is my scenerio. I want to use background task to send newsletter to subscribed users. This is done by MailService, which has UnitOfWork as dependency.

I tried the solution from learn.microsoft.com so in my case I use a method of IMailService instead of ILogger, but I'm getting an error:

System.InvalidOperationException: 'Cannot consume scoped service >'Fit4You.Core.Data.IUnitOfWork' from singleton >'Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor'.'

I don't want to make my UnitOfWork or DbContext with Singleton lifetime. Is it possible to consume somehow a dependency of MailService which is scoped UnitOfWork?

I know about IServiceScopeFactory but maybe don't know how to use it properly.

I'm using .NET Core 2.2 and build in interface IHostedService

ScopedMailService:

public class ScopedMailService : IScopedMailService
{
    private readonly IMailService mailService;

    public ScopedMailService(IMailService mailService)
    {
        this.mailService = mailService;
    }

    public void DoWork()
    {
        mailService.SendNewsletterToSubscribedUsers();
    }
}

ConsumeScopedMailService:

public class ConsumeScopedMailService : IHostedService
{
    private Timer timer;
    private readonly IMailService mailService;
    public IServiceProvider Services { get; }

    public ConsumeScopedMailService(IServiceProvider services, IMailService mailService)
    {
        Services = services;
        this.mailService = mailService;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        var startTimeSpan = GetStartTimeSpan();
        var periodTimeSpan = TimeSpan.FromSeconds(30);

        timer = new Timer(DoWork, null, startTimeSpan, periodTimeSpan);

        return Task.CompletedTask;
    }

    private void DoWork(object state)
    {
        using (var scope = Services.CreateScope())
        {
            var scopedMailService = scope.ServiceProvider.GetRequiredService<IScopedMailService>();
            scopedMailService.DoWork();
        }
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    public void Dispose()
    {
        timer?.Dispose();
    }

    private TimeSpan GetStartTimeSpan()
    {
        var currentTime = DateTime.Now.Ticks;
        var executeTime = DateTime.Today.AddHours(8)
                                        .AddMinutes(0)
                                        .Ticks;

        long ticks = executeTime - currentTime;

        if (ticks < 0)
        {
            ticks = ticks + TimeSpan.TicksPerDay;
        }

        var startTimeSpan = new TimeSpan(ticks);

        return startTimeSpan;
    }
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddDbContext<Fit4YouDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString")));

    services.AddScoped<IUnitOfWork, UnitOfWork>();
    services.AddTransient<IMailService, MailService>();

    services.AddHostedService<ConsumeScopedMailService>();
    services.AddScoped<IScopedMailService, ScopedMailService>();

    ...
}

MailService:

    public class MailService : IMailService
    {
        private readonly IUnitOfWork unitOfWork;
        public MailService(IUnitOfWork unitOfWork)
        {
            this.unitOfWork = unitOfWork;
        }

        public void SendNewsletterToSubscribedUsers()
        {
            // Some Code
        }
    }

Upvotes: 8

Views: 4761

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131237

The singleton ConsumeScopedMailService depends on IMailService through its constructor :

public ConsumeScopedMailService(IServiceProvider services, IMailService mailService)

IMailService may be transient but the class that implements it depends on a scoped service, IUnitOfWork. Indirectly, ConsumeScopedMailService ends up depending on a scoped service.

To fix this IMailService mailService should be removed. It's not used in the posted code anyway.

Upvotes: 4

Related Questions