WCF with a Ninject ArgumentNullException

I'm new to Ninject and trying to gauge how well it compares to Windsor Castle, which I am more familiar with. My application is a WCF service application hosted in IIS. As a result, I try to deploy the container / core and use NinjectServiceHostFactory to create my service class, etc. Unfortunately, instead I get an ArgumentNullException.

Here is the exception information:

System.ArgumentNullException Cannot be null. Parameter name: root at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) at Ninject.Extensions.Wcf.NinjectInstanceProvider.GetInstance(InstanceContext instanceContext, Message message) in C:\Development\ninject.extensions.wcf\source\Ninject.Extensions.Wcf\NinjectInstanceProvider.cs:line 75 at System.ServiceModel.Dispatcher.InstanceBehavior.GetInstance(InstanceContext instanceContext, Message request) at System.ServiceModel.InstanceContext.GetServiceInstance(Message message) at System.ServiceModel.Dispatcher.InstanceBehavior.EnsureServiceInstance(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 

And here is the code I use (only for what’s important):

In TheService.svc (without code):

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

In Global.asax.cs:

 public class Global : NinjectWcfApplication { protected override IKernel CreateKernel() { var kernel = new StandardKernel(new ServiceModule()); return kernel; } } 

In ServiceModule.cs:

 internal class ServiceModule : NinjectModule { public override void Load() { Bind<ITheService>().To<TheService>(); Bind<ITheRepository>().To<TheRepository>(); } } 

All this code is working fine. I put some diagnostics to track method calls and the CreateKernel method is called, followed by a call to the Load method in the ServiceModule, which returns, and then returns CreateKernel. However, I get the exception above when I try to call any of the service methods in TheService.

I use wsHttpBinding, and service links are all resolved just fine. Interfaces and implementation classes are valid. It seems that the problem occurs when the actual instance of the service is retrieved from the container / kernel.

What am I missing?

+7
ninject wcf
source share
2 answers

I had the same problem and found that the kernel was not installed in KernelContainer.Kernel (for version 2.2 https://github.com/ninject/ninject.extensions.wcf/blob/2.2.0.0/src/ Ninject.Extensions.Wcf / KernelContainer.cs ) or (for version 2.3 https://github.com/ninject/ninject.extensions.wcf/blob/2.3.0.0/src/Ninject.Extensions.Wcf/NinjectServiceHostFactory.cs ) in NinjectServiceHostFactory.SetKernel(IKernel kernel) . Not sure why this is not a magical quest, but setting it in the CreateKernel() method in Global.asax seems to fix it.

+5
source share

We faced a similar regression problem that caused this exception (i.e., Ninject previously worked, and then suddenly threw:

It cannot be null. Parameter Name: root in Ninject.ResolutionExtensions.GetResolutionIterator

Ultimately, it was tracked to an unrelated error in some ProtoBuf matching ProtoBuf code, which in turn prevented Ninject from loading Ninject .

Therefore, if you are tracking this, it is recommended that you run test packages for all of the following:

  • Static constructors and static readonly member initialization
  • AutoMapper Map Download
  • Serialization Boot Card
  • and etc.
0
source share

All Articles