Unity的ResolutionFailedException

时间:2013-07-08 21:22:31

标签: c# unity-container

我正在使用模式和实践' Unity将依赖项注入到我的对象中并且遇到了一个奇怪的(对我而言)问题。这是我的班级定义:

public class ImageManager : IImageManager 
{
    IImageFileManager fileManager;

    public ImageManager(IImageFileManager fileMgr)
    {
        this.fileManager = fileMgr;

    }
}

public class ImageFileManager : IImageFileManager
{
    public ImageFileManager(string folder)
    {
        FileFolder = folder;
    }
 }

这是注册我的课程的代码

container.RegisterInstance<MainWindowViewModel>(new MainWindowViewModel())
         .RegisterType<IPieceImageManager, PieceImageManager>(
              new InjectionConstructor(typeof(string)))
         .RegisterType<IImageFileManager, ImageFileManager>()
         .RegisterType<IImageManager, ImageManager>(
              new InjectionConstructor(typeof(IImageFileManager)));

我最初在后面的代码中解决了这个问题(我知道,它违背了目的。请耐心等待。)这样的XAML文件

IImageManager imageManager = MvvmViewModelLocator.Container.Resolve<IImageManager>(
    new ParameterOverride("folder", "/images"));

它有效。但是我为我的主视图创建了一个视图模型,当我将同一行复制到其中时,我得到一个例外。以下是两个最内在的例外:

InnerException: Microsoft.Practices.Unity.ResolutionFailedException
  HResult=-2146233088
  Message=Resolution of the dependency failed, type = "SwapPuzzleApp.Model.IImageManager", name = "(none)".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type IImageManager does not have an accessible constructor.

At the time of the exception, the container was:

Resolving SwapPuzzleApp.Model.IImageManager,(none)

        Source=Microsoft.Practices.Unity
        TypeRequested=IImageManager
        StackTrace:
             at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
             at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable`1 resolverOverrides)
             at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
             at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container, ResolverOverride[] overrides)
             at SwapPuzzleApp.ViewModel.MainWindowViewModel..ctor() in c:\Users\Carole\Documents\Visual Studio 2012\Projects\SwapPuzzle\SwapPuzzle\ViewModel\MainWindowViewModel.cs:line 17
             at SwapPuzzleApp.ViewModel.MvvmViewModelLocator..cctor() in c:\Users\Carole\Documents\Visual Studio 2012\Projects\SwapPuzzle\SwapPuzzle\ViewModel\MvvmViewModelLocator.cs:line 51
        InnerException: System.InvalidOperationException
             HResult=-2146233079
             Message=The type IImageManager does not have an accessible constructor.
             Source=Microsoft.Practices.Unity
             StackTrace:

             StackTrace:
                  at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context)
                  at lambda_method(Closure , IBuilderContext )
                  at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c__DisplayClass1.<GetBuildMethod>b__0(IBuilderContext context)
                  at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
                  at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
                  at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
                  at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)
             InnerException: 

我不确定问题是什么,因为ImageManager显然有一个公共构造函数。我认为这可能是由于路径无效,但如果我具体实例化对象,一切都会有效。

// this line has no problems
IImageManager imageManager = new ImageManager(new ImageFileManager("/images"));  

我还想知道在注册IImageManager时是否需要传入新的InjectionConstructor(typeof(string)),但它似乎没有帮助,为什么现在需要它而不是之前需要它?所以我很难过。这是我第一次尝试使用依赖注入,所以它可能是基本的东西。不过,我只是没有看到什么。

2 个答案:

答案 0 :(得分:3)

我使用this article中的示例作为我的向导。其中的例子对于介绍来说太先进了,或者在那个主题中有错误的信息。

在咨询其他来源(主要是PluarlSight)之后,我想出了一个更简单,更合理的解决方案。

container.RegisterInstance<TimerViewModel>(new TimerViewModel());
container.RegisterType<IPieceImageManager, PieceImageManager>();
container.RegisterType<IImageFileManager, ImageFileManager>
    (new InjectionConstructor("/images"));
container.RegisterType<IImageManager, ImageManager>();

答案 1 :(得分:2)

仔细查看错误消息。请注意这一部分:

 Message=The type IImageManager does not have an accessible constructor.

请注意,类型名称是IImageManager,不是 ImageManager。沿着这条线的某个地方你丢失了你的类型映射。

您注册FileImageManager也有问题,因为您没有在注册中指定文件夹参数,因此Unity不知道要传递什么字符串。