2017-12-05 15 views
-1

私は前に多くの人が直面した同じ問題に直面しています。ここでNinject:一致するバインディングがありません

私の登録ロジックは

static Bootstrapper() 
    { 
     Container = new StandardKernel(); 

     var dialogService = new DialogService(); 
     var mainWindow = new MainWindow(Messenger.Default, dialogService); 
     dialogService.Intialize(mainWindow.DialogContainer); 

     Container.Bind<MainWindow>().ToConstant(mainWindow); 
     Container.Bind<IDialogService>().ToConstant(dialogService); 
     Container.Bind<ILogger>().ToMethod((ctx) => LogManager.GetLogger("Main")); 
     Container.Bind<IMessenger>().ToConstant(Messenger.Default); 
     Container.Bind<IEntityService>().To<EntityService>(); 
     Container.Bind<EntityMasterDetailViewModel>().To<EntityMasterDetailViewModel>().InSingletonScope(); 
     Container.Bind<MainViewModel>().To<MainViewModel>().InSingletonScope(); 
    } 

され、そのクラスの唯一のコンストラクタですEntityMasterDetailViewModel

public EntityMasterDetailViewModel(
     IDialogService dialog, 
     IEntityService service) 
    { 
     _service = service; 
     _dialog = dialog; 
    } 

を解決するときに問題が発生します。私はまた、複数のコンテナを持つ可能性を排除しました(静的な読み取り専用を使用)。

クラスの登録に何が間違っているかわかりません。すべての依存関係がコンテナに定義されています。

今、私は次のエラーを取得するでしょう:

Error activating IDialogService 
    No matching bindings are available, and the type is not self-bindable. 
    Activation path: 
    2) Injection of dependency IDialogService into parameter dialog of 
    constructor of type EntityMasterDetailViewModel 
    1) Request for EntityMasterDetailViewModel 

誰もが何かを見つけることができますか?

乾杯、私は両方の依存関係を解決することができる1

EDIT:Container.Get<IDialogService>()Container.Get<IEntityService>()両方が正しいインスタンスを返します。したがって、EntityMasterDetailViewModelを登録する方法でなければなりません。私はいくつかの選択肢を試してみましたが、どれも働いた:

 Container.Bind<EntityMasterDetailViewModel>().ToSelf().InSingletonScope(); 
    Container.Bind<EntityMasterDetailViewModel>().ToMethod(_ => Container.Get<Entity<asterDetailViewModel>()).InSingletonScope(); 
+1

静的ブートストラップメソッドの最後に 'Get ()'を試しましたか?あなたのカーネルを作る場所( 'new StandardKernel();')のどこにコードがあるのか​​チェックしましたか? – BatteryBackupUnit

+2

あなたはいくつかの 'IDialogService'を持っていて、BootStrapperと' EntityMasterDetailViewModel'で異なるものを参照している可能性がありますか? – jbl

答えて

0

あなたのコメントをありがとう、彼らは私に何が悪かったのかのヒントを与えた:私は間違って静的コンストラクタを使用していた、呼び出しが取得しつつ、カーネルが正しく初期化されていません<>が発行されました。

私が実装したかったのは、シングルトンでした。だから私は正しいパターンのために物事を回した。今回は、すべての登録/バインディングが最初の呼び出しである<>()の前に発生しました。私はもはや例外/エラーを取得しません。

関連する問題