TypeResolver in Cli/Logging example does not adhere to the changed specs in #620
See original GitHub issueInformation
- OS: MacOS
- Version: 12.0.1
- Terminal: Terminal
Describe the bug
I am trying to run a simplified version of the HelloCommand
example in your logging example to no avail. When running the command, it says Error: Could not resolve type 'HelloCommand+Settings'.
I only get this when trying to pass a type registrar to my CommandApp
. I have the exact type registrar from the example and have simplified the hello command to even remove the serilog registration.
Code snippets and a link to a repoducable repository example below.
To Reproduce
Run this repo with dotnet run hello paul
.
if you change var app = new CommandApp(registrar);
to var app = new CommandApp();
it works, so it seems to be something about the TypeRegistrar
Expected behavior
I expect the command to return hello paul
like it does when I am not adding the registrar.
Screenshots Assuming this repo is more useful?
Additional context Here’s the code without going to the repo.
Program.cs
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
using Spectre.Console.Cli;
using SpectreFail;
var serviceCollection = new ServiceCollection();
// serviceCollection.AddSingleton<IFileSystem, FileSystem>();
var registrar = new TypeRegistrar(serviceCollection);
var app = new CommandApp(registrar); // works without registrar
app.Configure(config =>
{
config.AddCommand<HelloCommand>("hello");
});
return app.Run(args);
public class HelloCommand : Command<HelloCommand.Settings>
{
private IAnsiConsole _console;
public HelloCommand(IAnsiConsole console)
{
_console = console;
}
public class Settings : CommandSettings
{
[CommandArgument(0, "[Name]")]
public string Name { get; set; }
}
public override int Execute(CommandContext context, Settings settings)
{
AnsiConsole.MarkupLine($"Hello, [blue]{settings.Name}[/]");
// _console.MarkupLine($"Hello, [blue]{settings.Name}[/]");
return 0;
}
}
TypeRegistrar
namespace SpectreFail;
using Microsoft.Extensions.DependencyInjection;
using Spectre.Console.Cli;
public class TypeRegistrar : ITypeRegistrar
{
private readonly IServiceCollection _builder;
public TypeRegistrar(IServiceCollection builder)
{
_builder = builder;
}
public ITypeResolver Build()
{
return new TypeResolver(_builder.BuildServiceProvider());
}
public void Register(Type service, Type implementation)
{
_builder.AddSingleton(service, implementation);
}
public void RegisterInstance(Type service, object implementation)
{
_builder.AddSingleton(service, implementation);
}
public void RegisterLazy(Type service, Func<object> func)
{
if (func is null)
{
throw new ArgumentNullException(nameof(func));
}
_builder.AddSingleton(service, _ => func());
}
}
public class TypeResolver : ITypeResolver
{
private readonly IServiceProvider _provider;
public TypeResolver(IServiceProvider provider)
{
_provider = provider ?? throw new ArgumentNullException(nameof(provider));
}
public object Resolve(Type type)
{
return _provider.GetRequiredService(type);
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
Maybe, though it doens’t seem to be able to plug directly in to the above without some mods.
I was more thinking even just adding something like the below directly into spectre console for us to use as a default.
It was confusing to me when I saw it in the docs at first and had to poke around github to see if you guys had an example of it. Doesn’t seem to be anything out of the ordinary going on, so seems like it would be a safe default to add.
Keeping this issue open, as the Cli/Logging example still needs to be modified.