-
-
Notifications
You must be signed in to change notification settings - Fork 64
Closed
Labels
invalidThis doesn't seem rightThis doesn't seem right
Description
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.IKeyedServiceProviderwhile attempting to activateApp.NotifyCommand.'
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
Labels
invalidThis doesn't seem rightThis doesn't seem right