How to implement guide with dependency injection in asp.net core 6 - resolving scoped services in singleton
I'm trying to configure I've been working on this all day and I'm working on a personal project and I'm currently working with a question with dependency injection in my ASP.NET Core 6 application... I have a service that is registered as a singleton, but it needs to access a scoped service. This results in the following behavior: `want to consume scoped service 'MyApp.Services.IMyScopedService' from singleton 'MyApp.Services.MySingletonService'`. I've tried various approaches to resolve this scenario, including rethinking my service lifetimes, but I still need to get it to work as expected. Here's the relevant part of my `Startup.cs` where I configure the services: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddScoped<IMyScopedService, MyScopedService>(); services.AddSingleton<IMySingletonService, MySingletonService>(); } ``` In `MySingletonService`, I attempt to inject `IMyScopedService` like this: ```csharp public class MySingletonService : IMySingletonService { private readonly IMyScopedService _myScopedService; public MySingletonService(IMyScopedService myScopedService) { _myScopedService = myScopedService; } } ``` I understand that singleton services get instantiated once and live for the entire lifetime of the application, while scoped services are created once per request. Can anyone suggest a best practice for accessing scoped services within a singleton without running into lifetime issues? I've considered using a factory pattern to resolve the scoped service, but I'm not sure how to implement it correctly in this context. Any insights or examples would be greatly appreciated! My development environment is Ubuntu. Is there a better approach? My development environment is macOS. I'm developing on CentOS with Csharp. Any pointers in the right direction? For reference, this is a production service. Any examples would be super helpful.