Musashi_Miyamoto
Musashi_Miyamoto

Reputation: 71

Dependency Injection in Class Library(.NET Standard 5 ) and Missing Startup.cs

I want to use dependency injection in class library build on .NET Standard 5. Also the class library dont have the startup.cs file so i can't inject my dependencies.

Is there anything i am missing here?

Kindly advise.

Upvotes: 1

Views: 406

Answers (1)

Connell.O'Donnell
Connell.O'Donnell

Reputation: 3703

One option would be to write an extension method for IServiceCollection that registers your dependencies. Something like:

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddServices(this IServiceCollection services)
    {  
        return services
            .AddTransient<IService1, Service1>()
            .AddTransient<IService2, Service2>();
    }
}

It would be the responsibility of the application consuming your library to call this method to register the dependencies.

Upvotes: 1

Related Questions