Skip to content

FromKeyedServices attribute is ignored #182

@mrtska

Description

@mrtska

Here is a small reproduce code.

using ConsoleAppFramework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

[assembly: ConsoleAppFrameworkGeneratorOptions(DisableNamingConversion = true)]

var builder = Host.CreateApplicationBuilder(args);
var services = builder.Services;

services.AddSingleton<ITest, Test>();
services.AddKeyedSingleton<ITest, KeyedTest>("Key");

var app = builder.ToConsoleAppBuilder();
app.Run(args);

interface ITest {
    int Value { get; set; }
}

class Test : ITest {
    public int Value { get; set; } = 1;
}

class KeyedTest : ITest {
    public int Value { get; set; } = 2;
}

[RegisterCommands(nameof(TestCommand))]
class TestCommand([FromKeyedServices("Key")] ITest test) {
    public void Run() {
        // This value should be 2 but 1 displayed
        Console.WriteLine(test.Value);
    }
}

This is a ILSpy screenshot and there is not considered keyed services.

Image

I think the behavior is unintentional, could you fix it?

Thank you for always making things so convenient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions