C# 如何在lightinject中拦截工厂

C# 如何在lightinject中拦截工厂,c#,dependency-injection,aop,interceptor,light-inject,C#,Dependency Injection,Aop,Interceptor,Light Inject,我不知道该怎么办。试图截获具有构造函数注入的工厂时,invocationInfo.Procedure()总是失败 var container = new ServiceContainer(); container.Register<ICool,Cool>(); container.Register<ILogger, Logger>(); container.Register<IInterceptor, LoggingInterceptor>(); //Two

我不知道该怎么办。试图截获具有构造函数注入的工厂时,invocationInfo.Procedure()总是失败

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();
例外情况:

LightInject.dll中发生“System.InvalidCastException”类型的异常,但未在用户代码中处理

其他信息:无法将“System.Func`1[ConsoleApplication1.IAwesome]”类型的对象强制转换为“System.object[]”类型


很抱歉,我无法为Lightinject制作新标签。没有足够的rep://

我是LightInject的作者,当截取依赖于运行时参数(如Awesome类)的服务实例时,它已被确认为一个bug

这个bug已经被修复,我会在一个新的NuGet软件包可用后尽快发回这里

致意


Bernhard Richter

如果你注释掉添加拦截器的第6行,你会得到例外吗?我几乎不理解第5行(不知道Lightinject),但可能不是拦截器导致了异常,但您实例化的可怕变量是错误的(我认为它需要一个数字和一个工厂,您只提供了
100
)。查看“参数”可以理解第5行如果我对第6行(拦截器)进行注释,注入工作符合设计要求。是否有指向该问题的链接?感谢您的回复!
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;
    }
}