2017-05-16 12 views
1

なぜ循環依存関係のエラーが発生しているのかわかりません。私はまた、一見無作為に呼び出された多くの異なるアクティベーションパスとクラスを取得しています。依存関係のないバインディングで循環依存関係の問題が発生する

これは、1年以上働いていたシステムの新しい問題です。それは現時点で変化しており、問題がポップアップした場所を正確に把握するために変更をロールバックしようとする動きが活発になっています。

は、と思われます。これは、マルチスレッドおよび競合状態と関係があります。スレッドをさらに追加すると、エラーが頻繁に表示されます。

Ninjectによって呼び出されたアクティベーションパス/依存関係の1つが私の注意を引いたとき、私はこれを理解するのに苦労していました。 リストされた私の依存関係の1つは、依存関係自体がありませんでした。上記の例で

Ninject.ActivationException: Error activating IMetaValueProvider using binding from IMetaValueProvider to ExecutionOutputMetaValueProvider 
A cyclical dependency was detected between the constructors of two services. 

Activation path: 
    6) Injection of dependency IMetaValueProvider into parameter valueProviders of constructor of type MetaValueResolverFactory 
    5) Injection of dependency IMetaValueResolverFactory into parameter valueResolverFactory of constructor of type MessageExecutionContextFactory 
    4) Injection of dependency IMessageExecutionContextFactory into parameter executionContextFactory of constructor of type MessageProcessor 
    3) Injection of dependency IMessageProcessor into parameter messageProcessor of constructor of type MessageProcessingManager 
    2) Injection of dependency IMessageProcessingManager into parameter messageProcessingManager of constructor of type QueuePollerFactory 
    1) Request for QueuePollerFactory 

ExecutionOutputMetaValueProviderは全く依存性を有していません。

  1. どのように私は循環2つの依存関係が何であるかを正確に伝えることができます。以下は

    ExecutionOutputMetaValueProvider

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    
    namespace DealerVision.Messaging 
    { 
        public class ExecutionOutputMetaValueProvider : IMetaValueProvider 
        { 
         public string MetaKeyPrefix 
         { 
          get 
          { 
           return "Output"; 
          } 
         } 
    
         public object GetMetaValue(IMessageExecutionContext context, string key) 
         { 
          if(context.ExecutableMessage.Result == null) 
          { 
           throw new Exception($"{nameof(ExecutionOutputMetaValueProvider)} cannot get key \"{key}\" because the {nameof(ExecutableMessageBase)} does not have a result."); 
          } 
    
          if (context.ExecutableMessage.Result.ExecutionOutput.ContainsKey(key)) 
          { 
           return context.ExecutableMessage.Result.ExecutionOutput[key]; 
          } 
    
          return null; 
         } 
    
         public IEnumerable<string> GetPersistantKeys(IMessageExecutionContext executionContext) 
         { 
          if (executionContext.ExecutableMessage.Result == null) 
           return Enumerable.Empty<string>(); 
    
          List<string> keys = new List<string>(); 
    
          foreach (var kvp in executionContext.ExecutableMessage.Result.ExecutionOutput) 
          { 
           keys.Add($"{this.MetaKeyPrefix}.{kvp.Key}"); 
          } 
    
          return keys; 
         } 
        } 
    } 
    

    つの質問に、ソースのですか? Ninjectは、そのうちの1つだけに関係する両方の依存関係をリストしません。

  2. 依存関係が依存関係を持たない場合、どの依存関係も呼び出すことができますか。どのようにサイクルを持つことが可能でしょうか?

明らかに何か不足しています。どんな助けもありがとう。

+1

を導くことができ、問題を示す最小限の検証例を投稿してください。 – Steven

+0

asp.net、asp.net MVCを使用していますか? WCF? – BatteryBackupUnit

+0

@BatteryBackupUnit、いいえ、コンソールアプリです。 –

答えて

1

インスタンスを "ボトムアップ"または "依存関係を最初に"作成する必要があります。つまりExecutionOutputMetaValueProviderMessageExecutionContextFactoryの前に作成され、その前に再び作成されます。QueuePollerFactory

ここで、ninjectはExecutionOutputMetaValueProviderで停止します。これは、チェーンにさらにタイプを作成する原因となるためです。現時点では、それはどんなものなのかは正確には述べられていません。

ExecutionOutputMetaValueProviderには、依存関係がありませんと言って以来: 。あなたが間違っているかどうか

  • チェック:
    • は、複数のコンストラクタがありますか?なぜなら、ninjectがバインディングを持つパラメータを持つコンストラクタがあれば、パラメータを持たないものではなく、これを使用するからです。
    • プロパティまたはメソッドの注入はありますか?また、here
    • という同じ名前の2番目のタイプがあります。バインディングは実際には他のタイプのものであり、実際のものとは異なります。 (Rebindと条件付きバインディングもチェックしてください)。OnActivationの使用のための
  • チェック - DependencyCreation延長のこのタイプの
  • のインスタンス化に結合されたアクティベーションにつながることができ、使用も自明でないアクティベーションに
+0

私は 'ExecutionOutputMetaValueProvider'にソースを含めました。あなたは見ることができますが、依存関係はありません。このバインディングが循環的な依存性の問題を引き起こす可能性はありますか? –

+0

@RyanGriffith私が記事で言ったように、 'OnActivation'や' DependencyCreation'はそのような "依存関係"を隠すことができます - 注入されていないので。 – BatteryBackupUnit

関連する問題