profanis
profanis

Reputation: 2751

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; }

Upvotes: 5

Views: 7222

Answers (1)

shamp00
shamp00

Reputation: 11326

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.

Upvotes: 12

Related Questions