5
votes

I have gotten Ninject MVC to work with Ninject WCF to get it to work I added two lines to CreateKernel method

kernel.Bind<ServiceHost>().To<NinjectServiceHost>();
KernelContainer.Kernel = kernel;

The dependencies are injected, but the Dispose method is not called, I've tried both with InRequestScope and doing my own scope Func return OperationContext.Current as Scope object, but the dispose is not called.

Update 2012-09-05 This info is obsolete, for MVC and WCF download from Nuget Ninject.MV3 and Ninject.Extensions.WCF (3.x) the MVC Nuget template will add a Ninject bootstrapper NinjectWebCommon.cs and to add support in a svc for ninject eddit the svc xml (Right click / View markup) and add Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory" to the xml node

2
I'm up-voting this question because it shows how to use NInject for MVC and WCF at the same time, which is really hard to find a good example for.Ian McLaird
This info is obsolete, see editAnders

2 Answers

2
votes

I recommend using Ninject 3.0.0-rc2 available from Nuget. InRequestScope works for both Web and WCF requests side a side.

2
votes

I can't reply to Remo's answer because I'm a stackoverflow n00b but the issue with the 'System.Runtime.CompilerServices.ExtensionAttribute' exception is that it has moved from System.Core in .net 4.0 to mscorlib in .net 4.5. If you install .net 4.5 the problem disappears. I think something was built with .net 4.5 in the mix. https://github.com/ninject/Ninject.Web.Common/issues/3 Sounds like it will be fixed soon. As for the Wcf InRequestScope issue Remo gave me some help today on the google groups... "add a new module that derives from "Ninject.GlobalKernelRegistrationModule" and load it into the kernel. This will be fixed for the final release though."

i.e. something like...

public class MyModule : Ninject.GlobalKernelRegistrationModule<WcfRequestScopeCleanup>
{
}

//And where you load your modules...
kernel.Load<MyModule>();