4

Not sure what to do here. invocationInfo.Proceed() always fails when trying to Intercept a factory that has constructor injection.

var container = new ServiceContainer();
container.Register<ICool,Cool>();
container.Register<ILogger, Logger>();
container.Register<IInterceptor, LoggingInterceptor>();

//Two problem lines
container.Register<int, IAwesome>((factory, value) => new Awesome(value, factory.GetInstance<ICool>()));
container.Intercept(sr => sr.ServiceType == typeof(IAwesome), sf => sf.GetInstance<IInterceptor>());

var awesome = container.GetInstance<int,IAwesome>(100);
awesome.Yo();

fails at this method in my interceptor.

public class LoggingInterceptor : IInterceptor
{
    private ILogger _logger;
    public LoggingInterceptor(ILogger logger)
    {
        _logger = logger;
    }
    public object Invoke(IInvocationInfo invocationInfo)
    {
        var returnValue = invocationInfo.Proceed(); //Exception here
        return returnValue;
    }
}

Exception:

An exception of type 'System.InvalidCastException' occurred in LightInject.dll but was not handled in user code

Additional information: Unable to cast object of type 'System.Func`1[ConsoleApplication1.IAwesome]' to type 'System.Object[]'.

Sorry I couldn't make a new tag for Lightinject. Not enough rep :/

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
YurikoEX
  • 105
  • 2
  • 9
  • If you comment out the 6th line that adds the interceptor, do you get the exception? I hardly understand the 5th line (no idea about Lightinject), but maybe it's not the interceptor that causes the exception, but you are instantiating the awesome variable wrong (i think it needs both a number and a factory, you have provided only `100`). – zafeiris.m May 10 '14 at 20:28
  • The 5th line can be understood looking at the "Parameters" section of http://www.lightinject.net/#toc14 And if I comment out the 6th line(the interceptor) the injection works as designed. – YurikoEX May 12 '14 at 13:59

1 Answers1

8

I am the author of LightInject and it has been confirmed to be a bug when intercepting service instances that relies on runtime arguments such as the Awesome class.

The bug has been fixed and I will post back here as soon as a new NuGet package is available.

Best regards

Bernhard Richter

seesharper
  • 3,249
  • 1
  • 19
  • 23