I'm getting an error in 2.0 that I used not to get in .Net Core 1.1.
Cannot consume scoped service 'ArithmosDaily.Data.ArithmosContext' from singleton 'ArithmosDaily.IHangfireJobSchedulerService'.
I am using Hangfire, and I need it to access my database (so I inject my context through DI) to update the database with some data from an API on a schedule. So it appears the DbContext is instantiated as Scoped, and I am setting my Hangfire service as a Singleton. In Core 1.1, there was no check for Scoped nested inside a Singleton. I know this concept works, because I ripped it out of another of my projects in a Core 1.1 project and nesting the Context inside my Singleton service works fine. (Which is annoying me that Microsoft feels the need to control me just because I may do something bad).
So, I change my Hangfire Service from Singleton to Scoped, hoping since my DbContext is apparently Scoped, that Scoped inside Scoped would be fine and instead I get this error: Cannot resolve scoped service 'ArithmosDaily.IHangfireJobSchedulerService' from root provider.
I still have no clue what that one even means.
After doing a little bit of digging, I came across the two links at the bottom of my post (and so far that's all I can find). I tried setting services.BuildServiceProvider(false);
in my Startup as suggested in the first link, but that didn't change anything in my Configure: I still get the error when setting up my service.
I am totally stumped and not sure what else to try. Maybe I have code in the wrong spot? I'm not sure.
Here is my code for Startup and Configure:
public void ConfigureServices(IServiceCollection services)
{
services.BuildServiceProvider(false);
string conn = Configuration.GetConnectionString("Arithmos");
services.AddDbContext<ArithmosContext>(options => options.UseSqlite(conn));
services.AddMvc();
services.AddHangfire(x => x.UseSQLiteStorage(conn));
services.AddScoped<IHangfireJobSchedulerService, HangfireJobSchedulerService>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(OnStartup);
lifetime.ApplicationStopping.Register(OnShutdown);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
app.UseExceptionHandler("/Error");
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseHangfireServer();
app.UseHangfireDashboard(options: new DashboardOptions
{
Authorization = new[] { new HangfireAuthorizationFilter() }
});
jobSchedulerService = app.ApplicationServices.GetRequiredService<IHangfireJobSchedulerService>();
}
I have come across these questions but they haven't helped me too much:
ASP.Net Core 2 ServiceProviderOptions.ValidateScopes Property