- Home
- .NET code examples
- Keyed services for ASP.NET Core dependency injection example
Keyed services for ASP.NET Core dependency injection example
Keyed services is a feature released in .NET 8 for dependency injection in ASP.NET Core.
IEnumerable.
Software
As .NET 8 is currently in preview, you'll need the preview version of Visual Studio 2022 (version 17.8 preview) and .NET 8 SDK (RC 1 or above).
Get the application working
These are the steps to get the application working.
- Fill out the code example form. We will send you an email where you can download the code example.
You are now ready to go.
Open the project in Visual Studio
Open up RoundTheCode.KeyedServices.sln in Visual Studio 2022 Preview.
https://localhost:8002, and test out the endpoints.
The Swagger endpoints
When you hit https://localhost:8002, it will load the Swagger documentation. Here are the endpoints:
/api/minimal-api- Using keyed services in .NET 8 using Minimal APIs./api/web-api/dotnet-7- Using theIEnumerabletype to get a list of implementations of the same service used prior to .NET 8./api/web-api/dotnet-8- Using keyed services in a MVC/Web API endpoint using theFromKeyedServiceattribute./api/web-api/dotnet-8- Using keyed services in a MVC/Web API endpoint using theIServiceProviderinstance.
More information
Watch our video where we go ahead and talk through keyed services and how they work.
In addition, you can read more about keyed services in .NET 8 with our tutorial.
Give us your anonymous feedback regarding this page, or any other areas of the website.
Watch .NET tutorials
Subscribe to our YouTube channel
Related code examples
Dependency injection code example for ASP.NET Core
Download an ASP.NET Core dependency injection that allows you to register and test how the singleton, scoped and transient service lifetimes work.