Search code examples
wcfninjectninject-interception

Interception doesn't work with WCF and Ninject 3.0


I have problem with Ninject 3.0 and interception WCF method.

I'm using TaskService

<%@ ServiceHost Language="C#" Debug="true" Service="Fasade.TaskService"     Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"%>

and my configuration is:

var settings = new NinjectSettings {LoadExtensions = false};
IKernel kernel = new StandardKernel(
   new NinjectSettings {LoadExtensions = false},
   new WcfModule(),
   new DynamicProxyModule()
);

kernel.Bind<ITaskService>().To<TaskService>().InRequestScope().Intercept().With(new ServiceInterceptor());

ServiceInterceptor implement IInterceptor,

When i'm using UnitTest for kernel, ninject use DynamicProxy object for ITaskService implementaction, but it dosn't work in WCF.

I don't know why NinjectServiceHostFactory don't use DynamicProxy and Interception to return implementation of ITaskService.


Solution

  • Instead of

    kernel.Bind<ITaskService>().To<TaskService>().
        InRequestScope().Intercept().With(new ServiceInterceptor())
    

    you should change binding to

    Bind<TaskService>().ToSelf().
        InRequestScope().Intercept().With<ServiceInterceptor>();
    

    because in service file .svc there is implementation

    Service="Fasade.TaskService"