Register dependency of class library which is not directly referenced in ASP.NET Core Web API

174 Views Asked by At

I have an ASP.NET Core Web API project with class libraries. I can register dependency of class library which are directly added as references into the project. But I am not able to find way to register dependency of class library which are not directly added to the project.

For example, as you can see in following screenshot:

enter image description here

  1. It is the ASP.NET Core Web API project.

  2. It has a class library FirstClasslib project.

  3. The FirstClasslib project is added as reference to the ASP.NET Core Web API project.

  4. FirstClasslib project has dll reference of SecondClasslib project.

  5. SecondClasslib is also a library with interface and its implementation as shown in the following screenshot:

    enter image description here

I want to register dependencies of SecondClasslib through DI like I did for FirstClasslib. But since it is not directly linked to the ASP.NET Core Web API project, how can I do this?

Please find source code repo at https://github.com/sujadkar/WebApplication1

I tried BuildServiceProvider approach but many people are saying it is anti pattern and we should avoid. I want to resolve dependency of SecondClasslib using Dependency Injection.

Edit 1 - Instead of using the Extension method and calling method from the Startup.cs class Can we use the following approach?

In secondclasslib could we add the following

public class SecondFactory
{
    public static ISecondClass CreateProvider() {
        IServiceCollection serviceCollection = new ServiceCollection();
        serviceCollection.AddTransient(typeof(ISecondClass),typeof(SecondClassImplementation));
        ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
        ISecondClass secondClass = serviceProvider.GetRequiredService<ISecondClass>();
        return secondClass;
    }
}

In firstclasslib we can get instances using the following code,

public class FirstClassImplementation : IFirstClass
{
    public void SubmitRequest(int hello)
    {
        ISecondClass secondClass = SecondFactory.CreateProvider();
    }
}

Could you please tell me if this is the correct approach for dependency registration and getting instances?

1

There are 1 best solutions below

4
Ruikai Feng On

If you don't want your API project to add a reference to SecondClassLib, then in your FirstClassLib project, create an extension method like this:

public static class MyExtension
{
    public static IServiceCollection AddSecondClassLibServices(this IServiceCollection services)
    {
        // register all services here
        services.AddScoped<ISecondClass, SecondClassImplementation>();
        return services;
    }
}

Then in your Web API project, use:

builder.Services.AddSecondClassLibServices();

For multiple projects, you need a compose root project to register all services as above and your main project to refer to this project