Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject property injection not working

I am new in Ninject and I need some help to move on.

I have a solution that consists of web.form (presentation) and various other class libraries projects.

In web.form application inside NinjectWebCommon cs file I have the following

kernel.Bind<HttpContext>()
      .ToMethod(ctx => HttpContext.Current).InThreadScope();

kernel.Bind<HttpContextBase>()
      .ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InTransientScope();

kernel.Bind<MPIBE.DESTINATION.CORE.SiteContext>()
      .ToMethod(ctx => new MPIBE.DESTINATION.CORE.SiteContext(
                           new HttpContextWrapper(HttpContext.Current)
       ));

I am trying to get an instance of a class (following the constructor)

public SessionUtilities(SiteContext siteContext)
{
    _siteContext = siteContext;
}

and I noticed that i can get the instance only form web.forms application and I can't get from other projects (class libraries). Does this make any sense?

I am trying to get the instance via property injection

[Inject]
public SessionUtilities _sessionUtilities { get; set; }
like image 874
profanis Avatar asked Sep 30 '13 10:09

profanis


1 Answers

I suspect the class that contains your _sessionUtilities property is being created with new instead of via Ninject.

Ninject will only inject your _sessionUtilities property if the containing instance is also created by Ninject, either because it is created with kernel.Get() or because it is itself being injected.

like image 146
shamp00 Avatar answered Nov 12 '22 07:11

shamp00