Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject MVC together with WCF extension does not work with InRequestScope

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

like image 410
Anders Avatar asked Oct 10 '22 02:10

Anders


2 Answers

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>();
like image 103
Matt Murphy Avatar answered Oct 13 '22 12:10

Matt Murphy


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

like image 45
Remo Gloor Avatar answered Oct 13 '22 10:10

Remo Gloor