Visual Studio 2022 - scenarios CS8618 on Nullable Reference Types with Dependency Injection in ASP.NET Core 6
I'm working on a personal project and I'm stuck on something that should probably be simple... I'm working with the behavior `CS8618: Non-nullable property 'MyService' of value type 'MyService' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.` in my ASP.NET Core 6 project while using dependency injection. I've set up a constructor in my controller like this: ```csharp public class MyController : ControllerBase { private readonly MyService _myService; public MyController(MyService myService) { _myService = myService; } } ``` I've also registered `MyService` in the `Startup.cs` as follows: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddScoped<MyService>(); services.AddControllers(); } ``` This setup works without any issues, but as soon as I turn on Nullable Reference Types in my project settings, I get the CS8618 behavior. I've tried adding a null check in the constructor, but that doesn't seem to resolve the underlying scenario. I've also considered changing `_myService` to `MyService?` to make it nullable, but then I end up dealing with null checks everywhere, which feels messy. Is there a best practice to handle this scenario while keeping Nullable Reference Types enabled? What am I missing here? For context: I'm using C# on Windows. Any ideas what could be causing this? How would you solve this?