The linked code shows how to use generic host for executable projects, class libraries usually should not care about hosting per se.
For libraries usual approach is to provide a method, usually in namespace like MyLib.Extensions.DependencyInjection
which performs all the needed setup and registrations (good practice is to call TryAdd
instead of Add{Lifetime}
):
namespace MyLib.Extensions.DependencyInjection;
public static class MyLibRegistrationExtensions
{
// possibly add another parameter to provide/setup the SDK settings
public static IServiceCollection AddMyLib(this IServiceCollection services)
{
// registration goes here
return services;
}
}
Also this method can expose action to set the options for the library - public static IServiceCollection AddMyLib(this IServiceCollection services, Action<MyLibOptions> setOptions = null)
(for example to have an ability to configure URLs for different environments, though if number of library users is limited you can follow "convention" that corresponding URLs should be just set up in the configuration).
See also: