Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot consume scoped service from singleton

I'm trying to use DBContext in Hosted services but getting this error. I tried to follow this accepted answer but somehow its not working, I'm not sure what I'm doing wrong.

I'm new to .net please guide me into right direction.

Unhandled Exception: System.InvalidOperationException: Cannot consume scoped service 'StatusApp.Context.DBContext' from singleton 'Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor'.

public class TokenService : IHostedService
{
    public IConfiguration _Configuration { get; }
    protected IMemoryCache _cache;
    private Timer _timer;
    public IHttpClientFactory _clientFactory;
    public DBContext _DBcontext;

    private readonly IServiceScopeFactory _scopeFactory;

    public TokenService(IConfiguration configuration, IMemoryCache memoryCache, IHttpClientFactory clientFactory, DBContext DBcontext, IServiceScopeFactory scopeFactory)
    {
        _Configuration = configuration;
        _cache = memoryCache;
        _clientFactory = clientFactory;
        _scopeFactory = scopeFactory;
        _DBcontext = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<DBcontext>();
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _timer = new Timer(getOrg, null, 0, 1000);
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        //Timer does not have a stop. 
        _timer?.Change(Timeout.Infinite, 0);
        return Task.CompletedTask;
    }

    public async Task getOrg()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "organizations");
        var response = await _client_NP.SendAsync(request);
        var json = await response.Content.ReadAsStringAsync();
        OrganizationsClass.OrgsRootObject model = JsonConvert.DeserializeObject<OrganizationsClass.OrgsRootObject>(json);

        foreach (var item in model.resources)
        {
            var g = Guid.Parse(item.guid);
            var x = _DBcontext.Organizations.FirstOrDefault(o => o.OrgGuid == g);
            if (x == null)
            {
                _DBcontext.Organizations.Add(new Organizations
                {
                    OrgGuid = g,
                    Name = item.name,
                    CreatedAt = item.created_at,
                    UpdatedAt = item.updated_at,
                    Timestamp = DateTime.Now,
                    Foundation = 3
                });
            }
            else if (x.UpdatedAt != item.updated_at)
            {
                x.CreatedAt = item.created_at;
                x.UpdatedAt = item.updated_at;
                x.Timestamp = DateTime.Now;
            }
        }

        await _DBcontext.SaveChangesAsync();
    }
}
like image 866
Jojo Avatar asked Aug 20 '19 20:08

Jojo


1 Answers

You're almost there, but you've left DBContext as a dependency in TokenService's constructor. Remove that and you'll no longer receive the error.

public TokenService(
    IConfiguration configuration,
    IMemoryCache memoryCache,
    IHttpClientFactory clientFactory,
    DBContext DBcontext,
    IServiceScopeFactory scopeFactory)

However, you're not quite following the recommendation for dealing with DbContext's in a singleton service. Instead of creating a single instance of DBContext in the constructor and storing it as a field, create a scope and a corresponding DBContext whenever you need it. In your case, that's in the getOrg method.

Follow these steps to achieve that:

  1. Remove the _DBcontext field from your TokenService class:

    public DBContext _DBcontext;

  2. Remove the associated assignment from the TokenService constructor:

    _DBcontext = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<DBcontext>();

  3. In getOrg, create a scope, resolve an instance of DBContext and, lastly, dispose of the scope:

    public async Task getOrg()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "organizations");
        var response = await _client_NP.SendAsync(request);
        var json = await response.Content.ReadAsStringAsync();
        OrganizationsClass.OrgsRootObject model = JsonConvert.DeserializeObject<OrganizationsClass.OrgsRootObject>(json);
    
        using (var scope = _scopeFactory.CreateScope())
        {
            var dbContext = scope.ServiceProvider.GetRequiredService<DBcontext>();
    
            foreach (var item in model.resources)
            {
                var g = Guid.Parse(item.guid);
                var x = dbContext.Organizations.FirstOrDefault(o => o.OrgGuid == g);
                if (x == null)
                {
                    dbContext.Organizations.Add(new Organizations
                    {
                        OrgGuid = g,
                        Name = item.name,
                        CreatedAt = item.created_at,
                        UpdatedAt = item.updated_at,
                        Timestamp = DateTime.Now,
                        Foundation = 3
                    });
                }
                else if (x.UpdatedAt != item.updated_at)
                {
                    x.CreatedAt = item.created_at;
                    x.UpdatedAt = item.updated_at;
                    x.Timestamp = DateTime.Now;
                }
            }
    
            await dbContext.SaveChangesAsync();
        }
    }
    

Instead of using the _DBContext field, the code above creates a local, properly-scoped variable dbContext and uses that instead.

like image 165
Kirk Larkin Avatar answered Sep 17 '22 12:09

Kirk Larkin