Silviu Preda
Silviu Preda

Reputation: 628

Service Locator in Global.asax

I have several parts in my Global.asax where I need to use something like

var myServiceInstance = Kernel.Get<IMyServiceInterface>();

The app is MVC3, the service locator is NInject 2.2.0.0. However, when upgrading from NInject 2.1.0.0, the Kernel property became obsolete, and it states that NInject should not be used as a service locator.

I am more than willing to comply to this, but I need some guidance in the process. Can anyone tell me what the new approach should be?

I use service location in Global.asax because i need access to the database. This is the only fixed requirement.

L.E: is property injection a solution in this case?

Upvotes: 1

Views: 220

Answers (1)

Silviu Preda
Silviu Preda

Reputation: 628

I came up with this solution, since property injection was not a solution:

var myServiceInstance = DependencyResolver.Current.GetService<IMyServiceInterface>();

Is this acceptable?

Upvotes: 1

Related Questions