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.
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?
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With