Title: Cannot consume scoped service from singleton
Tags: c#;asp.net-core;.net-core;asp.net-core-mvc
Question: 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&gt;();
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&gt;(json);
foreach (var item in model.resources)
{
var g = Guid.Parse(item.guid);
var x = _DBcontext.Organizations.FirstOrDefault(o =&gt; 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();

Here is the accepted answer: 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:
Remove the _DBcontext field from your TokenService class:
public DBContext _DBcontext;
Remove the associated assignment from the TokenService constructor:
_DBcontext = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<DBcontext&gt;();
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&gt;(json);
using (var scope = _scopeFactory.CreateScope())
var dbContext = scope.ServiceProvider.GetRequiredService<DBcontext&gt;();
foreach (var item in model.resources)
{
var g = Guid.Parse(item.guid);
var x = dbContext.Organizations.FirstOrDefault(o =&gt; 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.
Comment for this answer: I'm getting error `Argument 1: cannot convert from 'method group' to 'TimerCallback'` in `StartAsync` function on `_timer` line, do you know by any chance whats wrong?
Comment for this answer: That's because the signature for the callback should be `void` but yours is `Task`. David Fowler has some awesome advice on handling this problem [here](https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#timer-callbacks).


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:08/20/19', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c#><asp.net-core><.net-core><asp.net-core-mvc>', 'Title': 'Cannot consume scoped service from singleton', 'ViewCount': 9411, 'id': 57580937, 'user_AboutMe': None, 'user_AccountId': 16388447.0, 'user_DisplayName': 'Jojo', 'user_Reputation': 107.0, 'user_Views': 18.0, 'user_id': 11837837.0}}