From the course: ASP.NET Core in .NET 6: Dependency Injection

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Forgetting to add a service

Forgetting to add a service

- [Instructor] We're now going to look at some common errors that occur when using dependency injection. The first one is forgetting to add a service. It's essential to register your services to the DI container. If you attempt to use the service and it has not been registered, the application will throw an exception. It's important to remember that a service is not initialized until it's used for the first time. So it may not be obvious that it hasn't been registered. Let's have a look and see what error we get when we forget to add the service. In our product controller, you can see that we are injecting the product service as a dependency. In our program class, we can see that we've added this class we have a transient scope lifetime. Let's go ahead and remove the registration and run the application. We can see that the application is running fine. Let's go ahead and go to our product page. We can see that when…

Contents