Unity错误---尝试获取IUserRepository类型的实例时出现激活错误,键“”

时间:2013-08-03 17:37:13

标签: c# asp.net-mvc visual-studio-2012 unity-container shrinkr

我正在尝试运行从codeplex

下载的MVC示例项目Shrinkr

我实际上在VS2012中打开了这个解决方案,并且正在成功编译。但是当我运行这个应用程序时,我收到了这个错误。

我收到一条异常错误消息,指出"在尝试获取IUserRepository类型的实例时出现激活错误,键"" "

下面粘贴的是详细的异常消息

   Microsoft.Practices.ServiceLocation.ActivationException was unhandled by user code
  HResult=-2146233088
  Message=Activation error occured while trying to get instance of type IUserRepository, key ""
  Source=Microsoft.Practices.ServiceLocation
  StackTrace:
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService]()
       at Shrinkr.Web.CreateDefaultUsers.ExecuteCore(IServiceLocator serviceLocator) in c:\Users\RON\Downloads\Shrinkr\Shrinkr.Web.Common\BootstrapperTask\CreateDefaultUsers.cs:line 19
       at System.Web.Mvc.Extensibility.BootstrapperTask.Execute(IServiceLocator serviceLocator)
       at System.Web.Mvc.Extensibility.Bootstrapper.Execute()
       at System.Web.Mvc.Extensibility.ExtendedMvcApplication.Application_Start()
  InnerException: Microsoft.Practices.Unity.ResolutionFailedException
       HResult=-2146233088
       Message=Resolution of the dependency failed, type = "Shrinkr.Repositories.IUserRepository", name = "". Exception message is: The current build operation (build key Build Key[Shrinkr.Infrastructure.EntityFramework.UserRepository, null]) failed: The parameter databaseFactory could not be resolved when attempting to call constructor Shrinkr.Infrastructure.EntityFramework.UserRepository(Shrinkr.Infrastructure.EntityFramework.IDatabaseFactory databaseFactory, Shrinkr.Infrastructure.EntityFramework.IQueryFactory queryFactory). (Strategy type BuildPlanStrategy, index 3)
       Source=Microsoft.Practices.Unity
       TypeRequested=IUserRepository
       StackTrace:
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
            at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name)
            at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name)
            at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t)
            at System.Web.Mvc.Extensibility.Unity.UnityAdapter.DoGetInstance(Type serviceType, String key)
            at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
       InnerException: Microsoft.Practices.ObjectBuilder2.BuildFailedException
            HResult=-2146233088
            Message=The current build operation (build key Build Key[Shrinkr.Infrastructure.EntityFramework.UserRepository, null]) failed: The parameter databaseFactory could not be resolved when attempting to call constructor Shrinkr.Infrastructure.EntityFramework.UserRepository(Shrinkr.Infrastructure.EntityFramework.IDatabaseFactory databaseFactory, Shrinkr.Infrastructure.EntityFramework.IQueryFactory queryFactory). (Strategy type BuildPlanStrategy, index 3)
            Source=Microsoft.Practices.ObjectBuilder2
            BuildKey=Build Key[Shrinkr.Infrastructure.EntityFramework.UserRepository, null]
            ExecutingStrategyIndex=3
            ExecutingStrategyTypeName=BuildPlanStrategy
            StackTrace:
                 at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
                 at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
                 at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
            InnerException: System.InvalidOperationException
                 HResult=-2146233079
                 Message=The parameter databaseFactory could not be resolved when attempting to call constructor Shrinkr.Infrastructure.EntityFramework.UserRepository(Shrinkr.Infrastructure.EntityFramework.IDatabaseFactory databaseFactory, Shrinkr.Infrastructure.EntityFramework.IQueryFactory queryFactory).
                 Source=Microsoft.Practices.ObjectBuilder2
                 StackTrace:
                      at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForResolutionFailed(Exception inner, String parameterName, String constructorSignature, IBuilderContext context)
                      at BuildUp_Shrinkr.Infrastructure.EntityFramework.UserRepository(IBuilderContext )
                      at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
                      at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
                      at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
                 InnerException: Microsoft.Practices.ObjectBuilder2.BuildFailedException

感谢您的建议,让它发挥作用

1 个答案:

答案 0 :(得分:0)

严格来说,我不确定它是什么问题,根据我的经验,如果你下载一个稳定的样本但无法运行它,大多数情况下它可能是参考问题。

因此,您可以检查是否缺少任何DLL文件并确保其路径为Asset / Plugins。然后,检查 Edit-ProjectSettings-Player-YourTargetPlatform-OtherSettings ,确认Api Compatibility Level设置为.NET2.0(默认为子集)。

希望它有效,祝你好运:)。