question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeResolver in Cli/Logging example does not adhere to the changed specs in #620

See original GitHub issue

Information

  • 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:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pdevito3commented, Jan 1, 2022

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.

public class DefaultTypeRegistrar : ITypeRegistrar
{
    private readonly IServiceCollection _builder;

    public DefaultTypeRegistrar(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);
    }
}
1reaction
nils-acommented, Jan 1, 2022

Keeping this issue open, as the Cli/Logging example still needs to be modified.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[binutils-gdb] Created tag users/hjl/linux/release/2.29.51.0.1
Tell the linker testsuite that lm32-rtems toolchains do not (*) 9c1ce10. ... Fix racy output matching in gdb.base/multi-attach.exp, gdb. (*) a60e073.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found