Add IConsole to DI
adambajguz opened this issue · 2 comments
adambajguz commented
Is it possible to inject IConsole instance to some service using Microsoft.Extensions.DependencyInjection?
Tyrrrz commented
Yes, you would do it like this:
var services = new ServiceCollection();
services.AddSingleton<IConsole>(new SystemConsole()); // or `new VirtualConsole();`, depending on what you need
var serviceProvider = services.BuildServiceProvider();
// ...
var console = serviceProvider.GetService<IConsole>();
var cli = new CliApplicationBuilder()
.AddCommandsFromThisAssembly()
.UseConsole(console) // make sure CliFx is using the same instance of IConsole
.Build()
adambajguz commented
Thanks, I think this can be added to the README.