3

Below code is to Initialize Dependency Injection Container in application global.asax

IUnityContainer container = new UnityContainerFactory().CreateConfiguredContainer();
var serviceLocator = new UnityServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

But application is always failing with parameter less constructor exception for HomeController, below is the exception.

Resolution of the dependency failed, type = "MyApp.Web.Controllers.HomeController", name = "(none)". Exception occurred while: while resolving.

Exception is: InvalidOperationException - The current type, Microsoft.Practices.ServiceLocation.IServiceLocator, is an interface and cannot be constructed. Are you missing a type mapping?

At the time of the exception, the container was:

Resolving MyApp.Web.Controllers.HomeController,(none) Resolving parameter "serviceLocator" of constructor MyApp.Web.Controllers.HomeController(Microsoft.Practices.ServiceLocation.IServiceLocator serviceLocator) Resolving Microsoft.Practices.ServiceLocation.IServiceLocator,(none)

Below is the inner exception of the exception.

at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\DynamicMethod\Creation\DynamicMethodConstructorStrategy.cs:line 207 at BuildUp_Microsoft.Practices.ServiceLocation.IServiceLocator(IBuilderContext ) at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 43 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 112 at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\BuilderContext.cs:line 215 at BuildUp_MyApp.Web.Controllers.HomeController(IBuilderContext ) at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 43 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 112 at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 511

Microsoft.Practices.Unity 2.0.414.0 has been used. What is the issue with implementation, am I missing something?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Hi10
  • 531
  • 1
  • 5
  • 21

2 Answers2

3

You need to register all interfaces your controller relies on. In this case

HomeController(Microsoft.Practices.ServiceLocation.IServiceLocator serviceLocator) {...}

Means you need to have line like

container.RegisterType<IServiceLocator, SomeServiceLocatorImpl>();

Note that it is generally better practice to depend on required interfaces instead on IServiceLocator - How to avoid Service Locator Anti-Pattern?

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • If I go with container.RegisterType(); this then don't it become fixed? I want to be flexible to control with config file – Hi10 Jan 19 '16 at 19:16
1

Your constructor presumably has the following signature

public class HomeController : Controller
{
  public HomeController( IServiceLocator locator )
  {
      ...

Unity follows the signature and tries to find a concrete type mapped to the interface, registered in your container. But there is none.

What you do however, is a mistake. You should be injecting actual dependencies to services / business objects, not to the infrastructure class a service locator is.

 public HomeController( ISomeService service, IAnotherService another )

Unity would resolve these, assuming you first map abstractions to concrete types.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • Now understood the role of servicelocator which has been implemented in application, its a wrong implementation, may be some one thought to inject servicelocator itself LOL.... Thanks Wiktor since service-locator getting initialized on app start I made bit change and rely what available in current context of service locator. – Hi10 Jan 19 '16 at 19:52
  • One question still roaming in my mind, why it was failing during http redirect or http post? – Hi10 Jan 19 '16 at 20:10
  • @Hi10 it will fail that way on every *request* that reaches that controller because how controllers work in ASP.Net MVC – Alexei Levenkov Jan 19 '16 at 20:48
  • But surprise is, first time application launch no issue things work but sudden navigation fails. I will investigate more thanks for the answer Alex. – Hi10 Jan 19 '16 at 21:49