Unable to get Ninject Interception to work with WCF

we are moving from UNITY to Ninject as our default service locator for WCF services. There's a beautiful NuGet package available for this, and getting standard resolution up and down is a breeze.

However - I want to intercept calls to my WCF service. Here is what I have:

My svc file:

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

Here is my service:

 public class Service1 : IService1 { [Inject] public ISomeManager Manager { get; set; } public string GetData(int value) { if(this.Manager != null) this.Manager.DoStuff(); return string.Format("You entered: {0}", value); } } 

The kernel is created as follows:

 private static void RegisterServices(IKernel kernel) { kernel.Bind<ISomeManager>().To<SomeManager>(); kernel.Bind<IService1>().To<Service1>().Intercept().With<MyInterceptor>(); } 

If I configure such a kernel, a manager instance is introduced, but the interception does not occur. My interceptor, which logs something before and after execution, is never called.

Other Stackoverflow threads suggest using:

  kernel.Bind<Service1>().ToSelf().Intercept().With<MyInterceptor>(); 

If I do this, the manager will not be introduced. If I then go over and create a constructor that accepts in the manager, it works, but then again: there is no interception.

  kernel.Bind<Service1>().ToSelf().WithConstructorArgument("manager", kernel.Get<ISomeManager>()).Intercept().With<MyInterceptor>(); 

What am I doing wrong here?

+4
source share
1 answer

Any methods that are intercepted must be virtual:

  public virtual string GetData(int value) 
0
source

Source: https://habr.com/ru/post/1496578/


All Articles