Ninjecting into App_Start classes and static classes

Let’s say you are doing some work on your AuthConfig class, in your Global.asax, or maybe a static class. You can’t property-inject, and generally it’s a pain to get this to work. However, if you’re using Ninject for ASP.NET MVC, you can use the following call: var myInjectedService = DependencyResolver.Current.GetService<IMyInjectedService>(); The performance impact of this…

Injecting MVC and HTTP attributes and filters using Ninject

The problem: null dependencies Ninject has some fun quirks. For instance, one “bug” I came across was that if you hit an ASP.NET controller with a custom attribute, the attribute’s constructor-injected dependencies would be null on startup. For example, take the following ActionFilterAttribute: public class ForceHttpAttribute : ActionFilterAttribute { private readonly IControllerContextHelper _controllerContextHelper; public ForceHttpAttribute(IControllerContextHelper controllerContextHelper)…

Ninject InSingletonScope still creates new objects

Me: Kernel.Bind<IDataCache>().To<InMemoryDataCache>().InSingletonScope(); Ninject: Haha, nah. Here’s a new object bro. I don’t know why exactly this happens, but for some reason I’ve seen it happen in more than one project. Binding an implementation to an interface in singleton scope just DOESN’T do that. Instead, with every injection, the constructor is called and we get a…