Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

ASP.NET MVC Service locator to inject dependencies into a web handler

I used to have a standard ASP.NET MVC 3 application that was Ninject-wired via WebActivators preAppStart hook. My controllers were dependency injected via constructor injection - classic.

However not I need to handle some request via .ashx web handler. As know from several discussions here on SO, the only way to inject into a handler is via service locator instance.

What if I need to inject exactly an instance that was instanciated in Ninject kernel for Controllers?

My ISessionChecker is bound to an XmlSessionChecker in Singleton scope in ninject module and the module is loaded into kernel and the kernel is in bootstraped into an ASP.NET MVC application at app_start.

Now I need my handlers to be locating this very same instance of XmlSessionChecker from global kernel. Can I somehow get this kernel as a service locator?

Upvotes: 2

Views: 547

Answers (1)

Maxim V. Pavlov
Maxim V. Pavlov

Reputation: 10509

Well, it turns out I could just use System.Web.Mvc.DependencyResolver.Current.GetService to locate my dependency, as I would in any other MVC 3 app's code.

Upvotes: 1

Related Questions