Resolve Keyed Services with Autofac in ASP.NET Core

If you ever dealt with dependency injection concept in any programming language, you probably know that it is really something that every complex application must deal with it. Let’s see the definition from Wikipedia then I talk about how to resolve keyed services with Autofac in asp.net core:

In software engineeringdependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client’s statePassing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.

A little about basics

In other words, it is there to make your life as a developer easier and let the application grow without to worry about drastic changes to the architecture.

For instance, you could have an interface that binds to a service and anytime you inject that interface to any class it just know how to resolve it, isn’t it awesome?

public interface IServiceProvider {
    public void setService();
}

// Client class
public class Client {
    private IServiceProvider _service;

    public Client(IServiceProvider service) {
        this.service = service;
    }
}

Once IServiceProvider binds to a service, Client class will have the service without having to know the implementation of the extended class.

That was just some basic overview of dependency injection, for sure, there are lots of other things to consider about binding and resolving contracts which hopefully I go over some of the main concepts in later posts.

Multiple contract bindings

But today I like to talk about one of the dependency injection frameworks called Autofac which fully support ASP.Net Core. If you ever used any DI frameworks you know that there are cases you like to bind your contracts to multiple implementation like below:

public interface ITestService
{
    string GetDate();
}

public class TestService : ITestService
{
    public string GetDate() {
        return "service";
    }
}

public class TestComponent : ITestService
{
    public string GetDate() {
        return "component";
    }
}

Then in your DI bindings for Autofac, let’s say you have the followings:

builder.RegisterType<TestService>().As<ITestService>().Keyed<ITestService>("service");
builder.RegisterType<TestComponent>().As<ITestService>().Keyed<ITestService>("component");

As it is obvious, I have two binding for the same interface, ITestService. I have designated a string for each binding to identify them later, “service” and “component”.

So far so good! (hopefully though). Now let’s say I have a class that injects ITestService and wants to use TestService implementation but not TestComponent one. How to do that?

There are multiple ways to do this but one of the easiest ways that I prefer and recommend is to use,KeyFilter that helps you to resolve proper implementation:

public class TestServiceConsumer
{
    private ITestService testService;    

    public TestServiceConsumer([KeyFilter("service")] ITestService testService)
    {
        this.testService = testService;
    }
}

As you can notice in the constructor for TestServiceConsumer we used KeyFilter to resolve desired class which is TestService.

Once the code runs, it Autofac and ASP.NET Core CLR resolve the contract and you will have TestService available to you, and that is how to resolve keyed services with Autofac in asp.net core

You may also like...

Leave a Reply

Your email address will not be published.