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

StackOverflow https://stackoverflow.com/questions/4297955

Question

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 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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top