Answer the question
In order to leave comments, you need to log in
How to form a collection of services not in the .ConfigureServices() lambda expression, but separately?
Good afternoon.
There is a standard mechanism for creating a host in .NET Core, as well as a standard mechanism for configuring services. It looks something like this:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
services.AddTransient<ITransientOperation, DefaultOperation>()
.AddScoped<IScopedOperation, DefaultOperation>()
.AddSingleton<ISingletonOperation, DefaultOperation>()
.AddSingleton<IConfigurator, Configurator>()
.AddTransient<OperationLogger>());
var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton<IConfigurationRoot>(configuration)
.AddTransient<ITransientOperation, DefaultOperation>()
.AddScoped<IScopedOperation, DefaultOperation>()
.AddSingleton<ISingletonOperation, DefaultOperation>()
hostBuilder.ConfigureServices((_, services) => services = serviceCollection);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question