Tyrrrz/CliFx

Add IConsole to DI

adambajguz opened this issue · 2 comments

Is it possible to inject IConsole instance to some service using Microsoft.Extensions.DependencyInjection?

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()

Thanks, I think this can be added to the README.