I'm working on ASP.NET Core 3.1 application. I want to perform some final actions related to database while my application is about to stop. To do that I'm trying to call a function in my scoped service which is a additional layer between database and ASP.NET Core.
Startup.cs
public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStopping.Register(() =>
{
app.ApplicationServices.GetService<MyService>().SynchronizeChanges();
});
}
But unfortunately i get Cannot resolve scoped service 'MyApplication.Services.MyService' from root provider error while I'm trying to do so.