I've been trying to achieve something with Ninject (without a great understanding of the library) and have realized it may not be possible to do what I want.
I've got one of our own projects that I've referenced, and was attempting to use Ninject to push some dependencies in, something like:
public class ImageHelper
{
[Inject]
public static AdaptiveImageSettings Settings { get; set; }
[Inject]
public static IImageSizerFactory Factory { get; set; }
}
}
The aim is to have some settings (which can be served by different classes) and a Factory who can create instances of an ImageHelper class. I'm not too caught up on what's static and what isn't right now.
If I try and use my ImageHelper from a WebApplication referencing that project however these properties are always null. From a Page in my WebApplication with the following the dependencies are injected fine:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[Inject]
public NetC.Core.ImageSizer.IImageSizerFactory Factory { get; set; }
}
From what I've read this is because the Kernel does get handled automatically, but I can't seem to figure out a way of getting access to the Kernel so I can resolve those properties. Can someone give me some pointers on what if this is reasonably possible, or what the next step might be? So far I've only seen the ServiceLocator anti-pattern and can't seem to find an extension that fits the bill.
Factory
in your page? Because that looks strange - you seem to claim that Ninject succesfully resolves the factory but doesn't resolve its internal dependencies. Of course, these properties are static, this could be a reason Ninject doesn't want to touch them. Did you try to have instance properties there? – BanjermasinOnPageInitComplete
with a simple call tokernel.Inject(page)
as described here. #4934195 However, resolving the property dependency should still work recursively and thus, I have no idea why instance properties on your image helper are not resolved. My idea would be to try constructor injection there. Could you try and report back? – Banjermasin