2

I am trying to use a decorator in Autofac with a delegate factory but I can't seem to get it to resolve the parameters.

public interface IFoo
{ }

public class Foo : IFoo
{
    public Foo(string bar)
    { ... }
}

public class DecoratedFoo : IFoo
{
    public DecoratedFoo(IFoo decorated)
    { ... }
}

I would like to inject into a service like so:

public SomeService(Func<string, IFoo> factory)
{
    // I would expect IFoo to be a DecoratedFoo here
    IFoo foo = factory("hello");
}

I have registered components like so:

builder.RegisterType<Foo>()
    .Named<IFoo>("foo")
    .UsingConstructor(typeof(string));

builder.RegisterDecorator<IFoo>(
    (ctx, inner) => new DecoratedFoo(inner),
    fromKey: "foo");

I get an error saying it cannot resolve my parameter bar. This is a simplified example but I won't know what the value of bar is (hence using the factory).

Is there any way to accomplish what I'm doing?

Dismissile
  • 32,564
  • 38
  • 174
  • 263

1 Answers1

1

When you use RegisterDecorator - it will not propagate string parameter you pass to your factory deeper to your Foo constructor. Here is an open issue about that. So on this line

IFoo foo = factory("hello");

It will throw exception (as you observe) because it will try to find parameterless constructor of Foo and there is none.

To fix, you can remove your RegisterDecorator and instead do that manually:

builder.RegisterType<Foo>()
    .Named<IFoo>("foo");            
builder.Register((cnt, parameters) => 
    new DecoratedFoo(cnt.ResolveNamed<IFoo>("foo", parameters))).As<IFoo>();  

Almost the same amount of code, but works as expected, because you manually propagate parameters.

Evk
  • 98,527
  • 8
  • 141
  • 191