2011-12-14 9 views
1

私のプロジェクトは移植可能な領域を使用して行われています。私はDI用にninjectを使用しています。他のアセンブリにあるクラスを注入していますので、areaRegistraction私のコンストラクタでninjectを使用して他のアセンブリにあるクラスを注入

DependencyResolver.Current.GetService<IModuleManager>().Add(this.module); 
IKernel kernel = DependencyResolver.Current.GetService<IKernel>(); 
kernel.Bind<IConfigurationRepository>().To<ConfigurationRepository>(); 

私はこのコードを持っている:

public RequestController(IconfigurationRepository configurationRepository) 
{ 
    this.configurationRepository= configurationRepository; 
} 

をしかし、何らかの理由によりconfigurationRepositoryは

nullであるしかし、私は置く場合:

public RequestController() 
{ 
    this.configurationRepository = ((StandardKernel)DependencyResolver.Current.GetService<IKernel>()).GetAll<IConfigurationRepository>().First(); 
} 

正常に動作します。彼らの違いは何ですか?

本当に感謝します。

+0

どのように正確に機能しませんか?それはあなたに2週間の予告か何を与えるのですか? –

+0

質問が更新されました。問題は、configurationRepositoryがヌルであることです。 – user1096760

答えて

3

複数のIConfigurationRepositoryが登録されていても、最初の1つが失敗すると、2番目の実装が動作します。

最初のケースでの例外は何ですか? 2番目のケースでFirstの代わりにSingleを使用するとどうなりますか?

+0

最初の場合、configurationRepositoryはnullです。私がシングルを使用している場合 – user1096760

+0

2番目にFirstの代わりにSingleを使用する場合は、例外を与えます。シーケンスには複数の要素が含まれています。 – user1096760

+2

そこに問題があります。 Ninjectは複数のバインディングを持つことを許しません。なぜなら、どちらを取るべきかがあいまいであるからです。 –

関連する問題