Dependency injection plays a crucial role in every programming language. Microsoft has introduced many new features and significant enhancements in .Net 8. “Keyed Services” is one of the important, powerful enhancements in the dependency injection shipped with .Net Core 8.
In this article, we will explore Keyed Services .Net 8, illustrating with an example. This article is suitable for beginners, intermediate learners, and professionals.
We are going to cover the following in this article.
- How do you deal with multiple implementations of the interface prior to Keyed Services?
- Introduction of Keyed Services?
- Lifetime scope for Keyed Services.
- AddKeyedSingleton
- AddKeyedScoped
- AddKeyedTransient
- How to register and resolve dependencies using Keyed Services
Prerequisites
Below are the prerequisites for Keyed Services.
- .NET 8 SDK (RC1 or later version)
- Visual Studio 2022 version 17.8 or later
Let’s start with the use case of the Keyed Services.
Let’s assume that we want to implement report generator functionality, which can generate reports on XML, CSV, and PDF as per the user's preference.
Now, think how you will implement this prior to .NET 8. Let’s create a demo MVC core project in .NET Core 6.0.
Step 1. Create a .NET Core MVC project using .NET Core 6.0.
Step 2. Create an Interface called “IReportGenerator”.
Step 3. Create Three classes “CSVReportGenerator”, “XmlReportGenerator”, and “And PDFReportGenerator” and implement the “IReportGenerator” interface.
CSVReportGenerator.cs
XmlReportGenerator.cs
PDFReportGenerator.cs
Step 4. Register dependencies in the program.cs file.
We have registered all three classes in the program.cs file.
Step 5. Make the code changes in the HomeController.
In the above code, we have injected “IReportGenerator” as a parameter in the controller constructor, which is then called the GenerateReport method in the index action method.
Step 6. Make code changes in the Index.cshtml file.
In the code below, we are printing the GeneratorReport output in the welcome message.
Step 7. Run the application and observe the output.
Output
We observed that only the last registered service was retrieved. In this case, the CSV report is the last registered service.
Now, the next question is how to get another registered service, if required, like XML Report or PDF Report. We can make the code changes below in the HomeController and achieve it.
Execute the program and see the output. This time, we will get the XmlReport.
Keyed Services allows us to register and retrieve dependency injection using Key.
You can register and retrieve multiple implementations of the same interface using a key.
Lifetime scope for Keyed Services
Microsoft introduced three new extension methods for the Lifetime scope of keyed services.
- AddKeyedSingleton
- AddKeyedScoped
- AddKeyedTransient
These extension methods are like AddSinglton, AddScoped, and AddTransient methods in .Net core, except it allow us to use “key” to define lifetime scope of dependencies.
Let’s create the same report example using .Net 8 Keyed Service.
Step 1. Create a .Net Core MVC project using .Net Core 8.0
Step 2. Create interface “IReportGenerator” and three classes “CSVReportGenerator”, “PDFReportGenerator”, and “XMLReportGenerator” in the previous section of Step 2 and Step 3.
Step 3. Register dependencies in the Program.cs file like below.
we have used “AddKeyedSingleton” to register dependencies.
Step 4. Make the below code changes in HomeController.
In this code, we have used [FromKeyedServices] to retrieve dependencies based on the Key.
Step 5. Copy index.cshtml code from the previous example, step 6.
Step 6. Execute the program and see the output.
Output
I hope you enjoyed this article and learned something new.
Best ASP.NET Core 8.0.8 Hosting Recommendation
At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.
0 comments:
Post a Comment