Using ServiceDescriptor to Register Dependencies in ASP.Net Core

Using ServiceDescriptor to Register Dependencies in ASP.Net Core

Registering dependencies in ASP.Net Core is really easy with the built-in DI Container, you just have to add them in to the IServiceCollection instance that is passed in using the appropriate Service Lifetimes and to do that you can use the provided extensions methods. Its pretty simple. But sometimes you may want to register dependencies using ServiceDescriptor. In this article we’ll look at how we can register dependencies using ServiceDescriptor and Where would this method be useful.

Creating the ServiceDescriptor Instance

You have to set the Implementation Type (the interface), the Service Type (the implementation of the interface) and the Service Lifetime of the service when you create the ServiceDescriptor. There are 4 ways of creating a new ServiceDescriptor instances.

You can new up and Instance of the ServiceDescriptor class using one of its constructors like below. You need to supply the Implementation Type, Service Type and the Service Lifetime as parameters.

var oneService = new ServiceDescriptor(typeof(IOneService), typeof(OneService), ServiceLifetime.Scoped);
services.Add(oneService);

Another way of creating a instance is using the Describe() static method. Again you have to provider the Implementation Type, Service Type and the Service Lifetime as parameters.

var twoService = ServiceDescriptor.Describe(typeof(ITwoService), typeof(TwoService), ServiceLifetime.Scoped);
services.Add(twoService);

Since the Service Lifetimes is a must to provide when creating the ServiceDescriptor there are static method available for each of the Service Lifetimes on the ServiceDescriptor Class. You can use them as well.

var threeService = ServiceDescriptor.Scoped(typeof(IThreeService), typeof(ThreeService));
services.Add(threeService);

Also, you can use the generic version of the static methods so that you can exclude the typeof operator.

var fourService = ServiceDescriptor.Scoped<IFourService, FourService>();
services.Add(fourService);

Once you have created the ServiceDescriptor instance, you need to add it to the Service Collection by using the Add() method. You will rarely need to register dependencies using this method, but its important to know when you need to use and where.

When to Use ServiceDescriptor to Register Dependencies

There are couple of instances where you need to create a ServiceDescriptor instance to register dependencies.

Replacing an Already Registered Dependency

If you want ever want to replace and already registered dependency, you can do it in ASP.Net Core dependency container. The Service Collection provides a Replace() method under the Microsoft.Extensions.DependencyInjection.Extensions namespace which only accepts a ServiceDescriptor.

services.Replace(ServiceDescriptor.Scoped<IFourService, FourService>());

Safely Register Multiple Implementations of a Dependency

You can register multiple implementations of a single Implementation Type in ASP.Net Core. But there is a possibility of duplicate registrations. ASP.Net Core will allow duplicate registrations to work and there can be scenarios where using duplicate registrations can be troublesome.

To avoid this issues you can use TryAddEnumerable() method on the ServiceCollection. This method accepts a single ServiceDescriptor Instance or an Enumerable of ServiceDescriptor. When using this method, any duplicate registrations will be skipped

services.TryAddEnumerable(ServiceDescriptor.Scoped<IThreeService, ThreeService>());
// or
services.TryAddEnumerable(new[] {
  ServiceDescriptor.Scoped<IThreeService, ThreeService>(),
  ServiceDescriptor.Scoped<IThreeService, AwesomeThreeService>(),
  ServiceDescriptor.Scoped<IThreeService, SuperAwesomeThreeService>()
});

Summary

In this article we learned about ServiceDescriptor in ASP.Net Core and how to use them when registering dependencies and also where using instances of ServiceDescriptor is useful. All the code sample in this article is available in GitHub under the following repository

You Might Also Like
Comments