2012-03-02 43 views
0

我收到以下錯誤,而:無參數的構造函數存在,但統一轉到另一個,而解決

mConsumerTokenManager = (IConsumerTokenManager)container.Resolve(typeof(IConsumerTokenManager), null); 

即使我有

public class InMemoryTokenManager : IConsumerTokenManager, IOpenIdOAuthTokenManager 
{ 
    private Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>(); 


public InMemoryTokenManager(){} 

    /// <summary> 
    /// Initializes a new instance of the <see cref="Ugi.Server.Authentication.Consumers.InMemoryTokenManager"/> class. 
    /// </summary> 
    /// <param name="consumerKey">The consumer key.</param> 
    /// <param name="consumerSecret">The consumer secret.</param> 
    public InMemoryTokenManager(string consumerKey, string consumerSecret) 
    { 
     if (String.IsNullOrEmpty(consumerKey)) 
     { 
      throw new ArgumentNullException("consumerKey"); 
     } 

     this.ConsumerKey = consumerKey; 
     this.ConsumerSecret = consumerSecret; 
    } 
Resolution of the dependency failed, type = "DotNetOpenAuth.OAuth.ChannelElements.IConsumerTokenManager", name = 

「(無)」 。 發生異常時:解析時。 異常是:InvalidOperationException - 無法構造字符串類型。您必須配置容器以提供此值。 ----------------------------------------------- At異常的時間,該容器是:

Resolving Ugi.Server.Authentication.Consumers.InMemoryTokenManager,(none) 

(映射從 DotNetOpenAuth.OAuth.ChannelElements.IConsumerTokenManager,(無))的 解決參數 「consumerKey」 構造Ugi.Server.Authentication.Consumers.InMemoryTokenManager (System.String consumerKey,System.String consumerSecret) 解決System.String,(無)

+0

見http://scottdepouw.com/blog/telling-unity-which-constructor-to-use -when-initializing-a-class /,也是http://codebetter.com/davidhayden/2008/10/28/specifying-injection-constructor-using-unity-fluent-interface-for-loose-coupling-and-poco / – 2012-03-02 16:34:27

回答

2

當目標類包含多個構造函數時,Unity將使用應用了InjectionConstructor屬性的類。如果 有多個構造函數,並且沒有任何構造函數攜帶了InjectionConstructor屬性,則Unity將使用帶有最多參數的構造函數 。如果有多個這樣的構造函數(更多 比具有相同參數數量的「最長」之一),Unity 將引發異常。

您可以在註冊時使用下面的代碼告訴統一使用哪個構造函數:

this.container.RegisterType<IConsumerTokenManager, InMemoryTokenManager>(new InjectionConstructor()); 
相關問題