I was trying to solve this for entire morning but seems that its time to ask for advice.
I have an MVC / WebApi / SignalR application. I have a service which I would like to start together with web application. And I would like this service to be injected. So this is what I am trying to do:
public static class NinjectWebCommon
{
...
private static void RegisterServices(IKernel kernel)
{
GlobalHost.DependencyResolver = new Microsoft.AspNet.SignalR.Ninject.NinjectDependencyResolver(kernel);
kernel.Bind<PricingService>().ToSelf().InSingletonScope();
}
}
public class Application : HttpApplication
{
protected void Application_Start()
{
...
PricingService pricingService = DependencyResolver.Current.GetService<PricingService>();
}
}
My problem is that the pricingService
variable is null.
According to this post this pattern should work but it does not. May be there is an influence from SignalR dependency resolver, but I am not really sure.
At the same time, when I'm using a constructor injection inside WebApi controller, injection works fine.
public class MyController : ApiController
{
private readonly PricingService _pricingService;
public MyController (PricingService pricingService)
{
_pricingService = pricingService;
}
}
Any help would be highly appreciated.