Pages

Sunday, August 6, 2017

Ninject IOC - ASP.NET WEB API

Nuget packages:
  • Ninject.Web.WebApi
  • Ninject.Web.WebApi.WebHost
  • Ninject.MVC5

<package id="Ninject" version="3.2.0.0" targetFramework="net461" />


<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net461" />


<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net461" />


<package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net461" />


<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net461" />


<package id="Ninject.Web.WebApi.WebHost" version="3.2.4.0" targetFramework="net461" />




NinjectWebCommon CreateKernal method

private static void RegisterServices(IKernel kernel)
{
     kernel.Bind<dbcontext>().ToSelf().InRequestScope();
     kernel.Bind<myclass>().To<myclass>();
}


Could not get property injection to work with Web API, so resorted using for property injection:

private myProperty = DependencyResolver.Current.GetService<myclass>();
public myProperty2 = DependencyResolver.Current.GetService<myclass2>();

DependencyResolver is in the namespace: System.Web.MVC. This is based on the current IOC container, so swapping out IOC containers would not break code.


This post is for the purpose of my notes only and sometimes a rant.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford

No comments:

Post a Comment