C#C
C#4mo ago
70 replies
Vortac

Using ` Microsoft.Extensions.DependencyInjection` in a library

I'm currently writing a wrapper around a REST API and would like to use Microsoft's DI library (normally I just use constructor injection) but I'm a bit confused as to how to do so. I've created a
ServiceCollectionExtensions
class and registered the services that I need:

public static class AirplanesLiveServiceCollectionExtensions
{
    public static IServiceCollection AddAirplanesLiveClient(this IServiceCollection services)
    {
        services
            .AddRefitClient<IAirplanesLiveApi>()
            .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));

        return services.AddTransient<AirplanesLiveClient>();
    }
}

However, I'm not sure what I need to do next. Do I then use this somehow in
Program.cs
of an example project which uses the library?
Was this page helpful?