开发者

Ninject interception extension with WCF gives me an "Object reference not set to an instance of an object." error

开发者 https://www.devze.com 2023-01-27 12:48 出处:网络
I\'m getting started with the Ninject interception extension and can\'t get it to work in my WCF service. With the WCF extension, ninject works fine, it\'s the interception that\'s giving me trouble.

I'm getting started with the Ninject interception extension and can't get it to work in my WCF service. With the WCF extension, ninject works fine, it's the interception that's giving me trouble. Maybe i'm doing it wrong? When I try to add 开发者_JAVA技巧the LinFuModel in the kernel constructor it tells me it's already loaded, so I guess that's good.

Basically all interception on the binding breaks my wcf service, but my methodinterception just works on the service (getData() is in the service contract).

edit: the following also doesn't work:

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

end edit

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 in advance, Rinze


LinFu supports only virtual method interception. Change all intercepted methods to virtual or switch to DynamicProxy2.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号