Difficulty in Implementing Dependency Injection in ASP.NET Core 6 with Singleton Services
I keep running into I'm wondering if anyone has experience with I'm wondering if anyone has experience with Hey everyone, I'm running into an issue that's driving me crazy... Does anyone know how to I tried several approaches but none seem to work. I'm working on a personal project and I'm working on an ASP.NET Core 6 application, and I've run into an scenario while trying to implement dependency injection for a singleton service. I have a service interface `IMyService` and its implementation `MyService` which I want to register as a singleton. However, when I try to resolve this service, I'm getting a `InvalidOperationException: want to consume scoped service 'MyProject.Services.IMyService' from singleton 'MyProject.Controllers.MyController'` behavior. Hereโs how I registered my services in `Startup.cs`: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IMyService, MyService>(); services.AddScoped<MyController>(); } ``` And hereโs the relevant part of `MyController`: ```csharp public class MyController : ControllerBase { private readonly IMyService _myService; public MyController(IMyService myService) { _myService = myService; } } ``` I need `MyController` to use `IMyService`, but it seems there's a conflict due to the differing lifetimes. After some research, I tried changing `MyController` to also be a singleton, but that led to other issues since it depends on a scoped service. Iโm unsure how to resolve this while following best practices for dependency injection. Should I modify the lifetime of `MyService`, or is there a better architectural way to handle this? Any insights or suggestions would be greatly appreciated! Thanks in advance! What's the correct way to implement this? This is my first time working with Csharp LTS. Any ideas what could be causing this? I'm coming from a different tech stack and learning Csharp.