使用WCF的Ninject拦截扩展为我提供了“未设置为对象实例的对象引用”。错误

时间:2010-11-28 17:14:05

标签: wcf ninject-2 ninject-extensions interception

我开始使用Ninject拦截扩展,无法让它在我的WCF服务中运行。使用WCF扩展,ninject工作正常,这是拦截给我带来麻烦。也许我做错了?当我尝试在内核构造函数中添加LinFuModel时,它告诉我它已经加载了,所以我猜这很好。

基本上所有对绑定的拦截都会破坏我的wcf服务,但我的methodinterception只适用于服务(getData()在服务契约中)。

编辑:以下内容也不起作用

  Kernel.Intercept((request) => request.Method.Name.StartsWith("Get"))
            .With<TimingInterceptor>(); 

结束修改

protected override IKernel CreateKernel()
    {
        IKernel kernel = new StandardKernel(new ServiceModule());

        //var binding = kernel.Bind<MockBroker>().ToSelf();
        //binding.Intercept().With<TimingInterceptor>(); // THIS BREAKS

        kernel.InterceptAfter<Watch>(m => m.GetData(0), i => { i.ReturnValue = "BLABLABLA"; log.Info("INTERCEPTED!"); }); //WORKS

        //kernel.Bind<Watch>().ToSelf().Intercept().With(new TimingInterceptor()); //BREAKS
        //kernel.Bind<FileSystemBroker>().ToSelf().Intercept().With<TimingInterceptor>(); //BREAKS
        return kernel;
    }

public class TimingInterceptor : SimpleInterceptor
{
    readonly Stopwatch _stopwatch = new Stopwatch();
    //private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    protected override void BeforeInvoke(IInvocation invocation)
    {
        _stopwatch.Start();
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        _stopwatch.Stop();
        string message = string.Format("[Execution of {0} took {1}.]",
                                        invocation.Request.Method,
                                        _stopwatch.Elapsed);
        //log.Info(message);
       _stopwatch.Reset();
    }
}

Thanx提前, Rinze

1 个答案:

答案 0 :(得分:1)

LinFu仅支持虚方法拦截。将所有截获的方法更改为虚拟或切换到DynamicProxy2。