Unity :没有为此对象定义无参数构造函数
本文关键字:定义 参数 构造函数 对象 Unity | 更新日期: 2023-09-27 18:30:58
下面的代码是在应用程序global.asax中初始化依赖注入容器
IUnityContainer container = new UnityContainerFactory().CreateConfiguredContainer();
var serviceLocator = new UnityServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
但是应用程序总是失败,参数较少的构造函数异常主控制器,下面是例外。
依赖项解析失败,类型 ="MyApp.Web.Controllers.HomeController",名称 ="(无)"。在以下情况下发生异常:解析时。
异常是:InvalidOperationException - 当前类型 Microsoft.Practices.ServiceLocation.IServiceLocator 是一个接口,无法构造。是否缺少类型映射?
在异常发生时,容器是:
解析 MyApp.Web.Controllers.HomeController,(none) 解析构造函数 MyApp.Web.Controllers.HomeController(Microsoft.Practices.ServiceLocation.IServiceLocator serviceLocator) 的参数"serviceLocator") 解析 Microsoft.Practices.ServiceLocation.IServiceLocator,(none)
下面是异常的内部异常。
at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForTryingToConstructInterface(IBuilderContext context) in e:''Builds''Unity''Unity''UnityTemp''Compile''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''Unity''UnityTemp''Compile''Unity''Src''ObjectBuilder''Strategies''BuildPlan''BuildPlan.cs BuildPlanStrategy:line 43 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:''Builds''Unity''Unity''UnityTemp''Compile''Unity''Src''ObjectBuilder''Strategies''StrategyChain.cs:line 112 at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) in e:''Builds''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''Unity''UnityTemp''Compile''Unity''Src''ObjectBuilder''Strategies''BuildPlan''BuildPlan.cs BuildPlanStrategy:line 43 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) in e:''Builds''Unity''Unity''UnityTemp''Compile''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''Src''UnityContainer.cs:line 511
Microsoft.Practices.Unity 2.0.414.0 已被使用。实施有什么问题,我错过了什么吗?
您需要注册控制器所依赖的所有接口。在这种情况下
HomeController(Microsoft.Practices.ServiceLocation.IServiceLocator serviceLocator) {...}
意味着你需要有像这样的线
container.RegisterType<IServiceLocator, SomeServiceLocatorImpl>();
请注意,通常更好的做法是依赖所需的接口而不是IServiceLocator
- 如何避免服务定位器反模式?
您的构造函数可能具有以下签名
public class HomeController : Controller
{
public HomeController( IServiceLocator locator )
{
...
Unity 遵循签名并尝试查找映射到接口的具体类型,并在容器中注册。但是没有。
然而,你的所作所为是一个错误。您应该将实际依赖项注入到服务/业务对象,而不是服务定位器所在的基础结构类。
public HomeController( ISomeService service, IAnotherService another )
Unity 将解决这些问题,假设您首先将抽象映射到具体类型。