WCF with Ninject throwing ArgumentNullException
Asked Answered
R

2

9

I am new to Ninject and trying to evaluate 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 am trying to spin-up the container/kernel and use the NinjectServiceHostFactory to create my service class, etc. Unfortunately, I'm getting an ArgumentNullException instead.

Here's 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 am using (pared down to only what's relevant):

In TheService.svc (no code-behind):

<%@ 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 of this code appears to work fine. I put some diagnostics in to trace the method calls and the CreateKernel method is called, followed by the call to the Load method in ServiceModule which returns then CreateKernel returns. However, I get the above exception when I try to call any of the service methods in TheService.

I'm using wsHttpBinding and the service references all resolve just fine. The interfaces and implementation classes are valid. It appears the problem is occurring when the actual service instance is being retrieved from the container/kernel.

What am I missing?

Revis answered 13/9, 2010 at 19:56 Comment(2)
I'd suggest trying the mailing list - the WCF factory stuff doesnt come up often here. Alternately, I'd you could try building from the sample bit by bit instead? (Dont have the source to hand to go examining the locations in your stacktrace and see what it could mean based on that). (I use the WCF stuff, but without the factory stuff)Mezzosoprano
The only thing different from the WcfTimeService example is that my service implementation is not in the code-behind of the .svc file. I've been considering downloading the source and seeing if I can step through to find the problem. I was hoping I wouldn't have to go through the trouble. What mailing list?Revis
A
6

I had the same problem and found that the Kernel was not set 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 isn't magically set, but setting it in the CreateKernel() method in Global.asax seems to fix it.

Appeasement answered 5/5, 2011 at 14:46 Comment(2)
Found the same problem answered in #5452080Leeannaleeanne
I've currently got the same issue. I've updated my code to look like this in my global asax for my WCF project: protected override IKernel CreateKernel() { IKernel standardKernel = new StandardKernel(new NinjectModule()); KernelContainer.Kernel = standardKernel; return standardKernel; } However, this does change the error that is listed above. Can you please point out if I'm missing something?Orangeism
I
0

We encountered a similar regression issue which raised this exception (i.e. Ninject was previously working, and then suddenly threw :

Cannot be null. Parameter name: root at Ninject.ResolutionExtensions.GetResolutionIterator

It was eventually tracked down to a an unrelated bug in some ProtoBuf mapping bootstrapping code, which in turn had prevented Ninject from bootstrapping correctly.

So would recommend that you run your test suites on all of the following when tracking this down:

  • Static constructors and static readonly member initialization
  • AutoMapper map bootstrapping
  • Serialization map bootstrapping
  • etc.
Interface answered 15/3, 2016 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.