简体   繁体   中英

Unity : No parameterless constructor defined for this object.

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.H omeController(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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM