我正在使用Patterns and Practices的Unity向我的對象中注入依賴關係,並且遇到了一個奇怪的問題(對我來說)。這裏是我的類定義:ResolutionFailedException與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"));
,如果我需要在新InjectionConstructor傳遞我也想知道(typeof運算(字符串))當我註冊IImageManager,但它似乎並沒有幫助,爲什麼現在會而不是之前需要呢?所以我很難過。這是我第一次使用依賴注入的嘗試,所以它可能是一些基本的東西。雖然我只是沒有看到什麼。
您正在解析IImageManager並將參數傳入名爲「文件夾」的類型爲string的構造器。但ImageManager沒有與該定義相匹配的構造函數; ImageManager的構造器接受IImageFileManager。 –
嗯。這看起來有點奇怪,但它在我將它放在MainWindow.xaml.cs中時工作,所以我認爲這條線是正確的。任何猜測爲什麼它會在一個文件而不是另一個文件中工作? – VanillaBean
我獨自離開了幾個月,最後回到了它。我現在看到我做的事情非常非常錯誤。 – VanillaBean