Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyed registrations don't work on concrete types #1250

Closed
tillig opened this issue Jan 26, 2021 · 2 comments
Closed

Keyed registrations don't work on concrete types #1250

tillig opened this issue Jan 26, 2021 · 2 comments

Comments

@tillig
Copy link
Member

tillig commented Jan 26, 2021

Describe the Bug

All of our examples and tests for key filtering use service interfaces, like:

builder.RegisterType<ConsoleLogger>().Keyed<ILogger>("console");

However, it turns out if you try using the concrete type as the keyed service, it fails when used with attribute filtering.

builder.RegisterType<ConsoleLogger>().Keyed<ConsoleLogger>("console");

Steps to Reproduce

In this unit test, you'll get a DependencyResolutionException where it fails to resolve the IdentifiableObject first parameter by key filter.

[Fact]
public void KeyFilterIsAppliedOnMultipleIndividualConcreteDependencies()
{
    var builder = new ContainerBuilder();
    builder.RegisterType<ConsoleLogger>().As<ILogger>();
    builder.Register(ctx => new IdentifiableObject { Id = "a" }).Keyed<IdentifiableObject>("First");
    builder.Register(ctx => new IdentifiableObject { Id = "b" }).Keyed<IdentifiableObject>("Second");
    builder.Register(ctx => new IdentifiableObject { Id = "c" }).Keyed<IdentifiableObject>("Third");
    builder.RegisterType<ManagerWithManyIndividualConcrete>();

    var container = builder.Build();
    var resolved = container.Resolve<ManagerWithManyIndividualConcrete>();

    Assert.IsType<ConsoleLogger>(resolved.Logger);
    Assert.Equal("a", resolved.First.Id);
    Assert.Equal("b", resolved.Second.Id);
    Assert.Equal("c", resolved.Third.Id);
}

public interface ILogger
{
}

public class ConsoleLogger : ILogger
{
}

public class IdentifiableObject
{
    public string Id { get; set; }
}

public class ManagerWithManyIndividualConcrete
{
    public ManagerWithManyIndividualConcrete(
        ILogger logger,
        [KeyFilter("First")] IdentifiableObject first,
        [KeyFilter("Second")] IdentifiableObject second,
        [KeyFilter("Third")] IdentifiableObject third)
    {
        Logger = logger;
        First = first;
        Second = second;
        Third = third;
    }

    public ILogger Logger { get; set; }

    public IdentifiableObject First { get; set; }

    public IdentifiableObject Second { get; set; }

    public IdentifiableObject Third { get; set; }
}

I'll add this failing test as skipped to the repo so we can troubleshoot.

Expected Behavior

You should be able to apply keys and filters to the component exposed as itself, not just as a different service.

Exception with Stack Trace

Autofac.Core.DependencyResolutionException : An exception was thrown while activating Autofac.Test.Features.AttributeFilters.WithAttributeFilterTestFixture+ManagerWithManyIndividualConcrete.
---- Autofac.Core.DependencyResolutionException : None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Autofac.Test.Features.AttributeFilters.WithAttributeFilterTestFixture+ManagerWithManyIndividualConcrete' can be invoked with the available services and parameters:
Cannot resolve parameter 'IdentifiableObject first' of constructor 'Void .ctor(ILogger, IdentifiableObject, IdentifiableObject, IdentifiableObject)'.

Dependency Versions

Autofac: develop at 9a2d3a4

@tillig
Copy link
Member Author

tillig commented Jan 26, 2021

I did go back and add a similar test to Autofac.Extras.AttributeMetadata and it passes, so it's something different in how it's handled here.

tillig added a commit that referenced this issue Jan 26, 2021
@tillig
Copy link
Member Author

tillig commented Jan 26, 2021

Hang on, I'm a flipping idiot. WithAttributeFiltering() was missing.

@tillig tillig closed this as completed Jan 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant