2011-12-14 10 views
0

私はエラーを取得する:下のある方法でコンストラクタのために型を解決する際にエラーが発生しましたか?

Resolution of the dependency failed, type = "MyAppApp.ServiceAgents.IMyAppServiceAgent", name = "(none)". 
Exception occurred while: while resolving. 
Exception is: InvalidOperationException - The type Int32 cannot be constructed. You must configure the container to supply this value. 
----------------------------------------------- 
At the time of the exception, the container was: 

    Resolving MyAppApp.ServiceAgents.MyAppServiceAgent,(none) (mapped from MyAppApp.ServiceAgents.IMyAppServiceAgent, (none)) 
    Resolving parameter "AuthHandlerId" of constructor MyAppApp.ServiceAgents.MyAppServiceAgent(System.Int32 AuthHandlerId, System.String AuthSessionGuid, System.ServiceModel.EndpointAddress ServiceEndPointAddress) 
    Resolving System.Int32,(none) 

internal ServiceLocator() 
     { 
      services = new Dictionary<object, object>(); 
      // fill the map 

      this.services.Add(typeof(IMyAppServiceAgent), _container.Resolve<IMyAppServiceAgent>()); 


     } 

これは私がMVVMから、私は(ViewModelLocatorで標準的な方法を持っている

このメソッドを呼び出す方法ですライトツールキット)メソッド

public static void CreateShowroomLog() 
     { 
      if (_showroomLog == null) 
      { 
       _showroomLog = new ShowroomLogViewModel(ServiceLocator.Instance(_container).GetService<IMyAppServiceAgent>()); 
      } 
     } 

コンストラクタは

public ViewModelLocator() 
     { 
      _container=new UnityContainer(); 
      _container.RegisterType<IMyAppServiceAgent, MyAppServiceAgent>(); 
     } 

私はインスタンスを必要としているのクラスがある:

protected static EndpointAddress ServiceEndPointAddress 
     { 
      get { return (App.Current as App).ServiceEndpointAddr; } 

     } 

     protected static string AuthSessionGuid 
     { 
      get { return (App.Current as App).W2OGuid; } 
     } 

     protected static int AuthHandlerId 
     { 
      get { return (App.Current as App).OriginalHandlerId; } 
     } 
public MyAppServiceAgent(int AuthHandlerId, string AuthSessionGuid, System.ServiceModel.EndpointAddress ServiceEndPointAddress) 
     { 
      _proxy = new MyAppService.Service1Client(new BasicHttpMessageInspectorBinding(new SilverlightAuthMessageInspector(AuthHandlerId.ToString(), AuthSessionGuid)), ServiceEndPointAddress); 
     } 

     public MyAppServiceAgent() 
      : this(AuthHandlerId, AuthSessionGuid, ServiceEndPointAddress) 
     { 

     } 

どのように私はcosntructorでこの問題を解決することができますか?

答えて

1

タイプを登録するときに、MyAppServiceAgentで呼び出すコンストラクタが指定されていませんでした。デフォルトでは、Unityは最も多くのパラメータを持つコンストラクタを選択しますが、それらのパラメータをどのように解決するかを指定しませんでした。

あなたは、私はさらに良いことだと思う何

_container=new UnityContainer(); 
_container.RegisterType<IMyAppServiceAgent, MyAppServiceAgent>(new InjectionConstructor()); 

。これを試してみて、それがこのタイプが解決されたときに呼び出されるMyAppServiceAgentのデフォルトコンストラクタ(paramaterless)の原因になりますかどうかを見ることができ取り除くことですMyAppServiceAgentクラスのServiceEndPointAddress、AuthSessionGuidおよびAuthHandlerId静的プロパティ次に、このような型を登録します。

_container=new UnityContainer(); 
_container.RegisterType<IMyAppServiceAgent, MyAppServiceAgent>(
    new InjectionConstructor(
     (App.Current as App).OriginalHandlerId, 
     (App.Current as App).W2OGuid, 
     (App.Current as App).ServiceEndpointAddr 
    ));  

これは、このコンストラクターを呼び出す必要があります。

public MyAppServiceAgent(int AuthHandlerId, string AuthSessionGuid, System.ServiceModel.EndpointAddress ServiceEndPointAddress) 
    { 
     _proxy = new MyAppService.Service1Client(new BasicHttpMessageInspectorBinding(new SilverlightAuthMessageInspector(AuthHandlerId.ToString(), AuthSessionGuid)), ServiceEndPointAddress); 
    } 

あなたのMyAppServiceAgentクラスはAppクラスに依存しません。

関連する問題