Thanks to visit codestin.com
Credit goes to github.com

Skip to content

App failed to start due to unable to resolve service for type 'Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider' while attempting to activate a Command/Class #148

@quyenhm

Description

@quyenhm

Version

v2.3.5

Platform

.NET 8 / Windows 11

Steps to reproduce

I have written a minimal code to reproduce this issue. Please try running it.

using CliFx;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;

namespace App;

public static class Program
{
    public static async Task<int> Main(params string[] args)
    {
        ServiceCollection services = [];

        services.AddSingleton<NotifyCommand>();

        services.AddKeyedSingleton<INotificationService, SmsNotificationService>("sms");
        services.AddKeyedSingleton<INotificationService, EmailNotificationService>("email");

        ServiceProvider serviceProvider = services.BuildServiceProvider();

        int exitCode = await new CliApplicationBuilder()
            .AddCommandsFromThisAssembly()
            .UseTypeActivator(serviceProvider)
            .Build()
            .RunAsync(args)
            .ConfigureAwait(false);

        return exitCode;
    }
}

[Command(Description = "Notify user")]
public class NotifyCommand(IKeyedServiceProvider keyedServiceProvider) : ICommand
{
    [CommandParameter(0, Description = "Notification Service")]
    public required string NotificationService { get; init; }

    [CommandOption("message", 'm', Description = "Notification Message")]
    public required string Message { get; init; }

    public ValueTask ExecuteAsync(IConsole console)
    {
        var service = keyedServiceProvider.GetKeyedService<INotificationService>(NotificationService)
            ?? throw new CommandException($"Invalid Notification Service '{NotificationService}'");

        service.Notify(Message);

        return default;
    }
}

public interface INotificationService
{
    string Notify(string message);
}

public class SmsNotificationService : INotificationService
{
    public string Notify(string message) => $"[SMS] {message}";
}

public class EmailNotificationService : INotificationService
{
    public string Notify(string message) => $"[Email] {message}";
}

Details

  • Expected behavior: The app can run without any errors.

  • Actual behavior: Encouterd an System.InvalidOperationException: 'Unable to resolve service for type Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider while attempting to activate App.NotifyCommand.'

image

Checklist

  • I have looked through existing issues to make sure that this bug has not been reported before
  • I have provided a descriptive title for this issue
  • I have made sure that this bug is reproducible on the latest version of the package
  • I have provided all the information needed to reproduce this bug as efficiently as possible
  • I have sponsored this project
  • I have not read any of the above and just checked all the boxes to submit the issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions