Describe the Bug
The priority logic for selecting types in the decorator pattern is incorrect.
Steps to Reproduce
var builder = new ContainerBuilder();
builder.RegisterType<A>().As<IA>();
builder.RegisterType<Ab>().As<IAb>();
builder.RegisterDecorator<D, IA>();
/*builder.RegisterDecorator<IA>(
(context, parameters, instance) =>
{
Console.WriteLine(instance.GetType());
Console.WriteLine(context.Resolve<IAb>().GetType());
return new D(context.Resolve<IAb>(), instance);
});*/
var container = builder.Build();
var d = container.Resolve<IA>();
interface IA;
interface IAb : IA;
class A : IA;
class Ab : IAb;
record D(IAb ab, IA a) : IA;
Expected Behavior
Use the correct registered types to inject into D and let D replace IA.
Exception with Stack Trace
Unhandled exception. Autofac.Core.DependencyResolutionException: An exception was thrown while activating D.
---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(IAb, IA)' on type 'D'.
---> System.InvalidCastException: Unable to cast object of type 'A' to type 'IAb'.
at lambda_method3(Closure, Object[])
at Autofac.Core.Activators.Reflection.BoundConstructor.Instantiate()
--- End of inner exception stack trace ---
Dependency Versions
Autofac: Autofac: 8.2.1
Additional Info
- The issue arises when Autofac attempts to resolve IA using RegisterDecorator. The decorator D expects an IAb parameter, but Autofac incorrectly attempts to provide an instance of A (which only implements IA), leading to an InvalidCastException.
- The Ab class is explicitly registered as IAb, so it should be recognized as a valid implementation for both IA and IAb.
Describe the Bug
The priority logic for selecting types in the decorator pattern is incorrect.
Steps to Reproduce
Expected Behavior
Use the correct registered types to inject into D and let D replace IA.
Exception with Stack Trace
Dependency Versions
Autofac: Autofac: 8.2.1
Additional Info