3
votes

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?

2
have you defined constructor inside controller? - Ehsan Sajjad
Not without parameter, unity should inject type with defined parameter constructor. - Hi10
where have you registered type that will be injected as parameter in constructor? - Ehsan Sajjad
Why are you injecting the ServiceLocator instead of the actual dependencies? - haim770
Type mapping is in config file. - Hi10

2 Answers

1
votes

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.

3
votes

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?