Automatically Registering Services With The Built In .Net Core Container

Friday, 28 September 2018

Scrutor is a library that provides a fluent syntax to configure automatic types registration with the built in .NET Core container. This guide will show how to use Scrutor to perform group registrations instead of registering each type individually.

The code used here continues from the post on how to create a self contained long running .NET Core console App and can be found here.

Create additional types

The first thing we''ll do is add additional types to the application. Create 3 new classes in a new folder called animals:

  • A class called Cat that implements an interface called ICat with a method called Speak()
  • A class called Dog that implements an interface called IDog with a method called Speak()
  • A class called Cow that implements an interface called ICow with a method called Speak()

The implementation of the Speak method should be the same in all classes. They will all write a message to the console.

Updating the application

Now we have our new services we can add them to the application. Create properties for the animals in the Application class then initialise them using constructor injection.

Update the WorkAsync method to make each of the animals speak and reduce the wait time to 3 seconds.

Running the application now will throw an error as we have not registered our new types with the container.

Registering types with Scrutor

Without Scrutor the registration code for our new types would look like this.

We would have to register each service individually as .NET Core does not provide a way to group registrations as most other containers do. This would also mean that as our application grows we would need to remember to update the container configuration to register any new services we create.

Add Scrutor to the application by running the following command in the package manager console.

install-package Scrutor

Scrutor adds more extension methods to the IServicesCollection interface to allow us to group our registrations. They all come under the Microsoft.Extensions.DependencyInjection namespace so we don''t need to add a using statement to access them.

Now we can add the registration code to the ContainerSetup class. Add the following code above the code already in the Run method:

This code retrieve all classes in the same assembly as the interface ICat and register them with the interfaces they implement with a lifetime of transient.

Now we can run the application. Put a breakpoint at the end of this method then run the application. If you inspect the services collection you''ll see we have some services registered.

We actually have 5 registrations instead of 4. Expanding the results view will show that the IApplication has been registered twice.

We can change our code to only register types that are in the same namespace as ICat. Change the code in the container setup class to the following

Now when we start the application and inspect the services collection we can see we have the correct number of services.

Remove the breakpoint and let the application continue execution. The animals now speak forever.

Any new classes added to the animals folder will now be registered automatically.