2011-12-14 100 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>()); 


     } 

这是我如何调用该方法

我在ViewModelLocator的标准方法(从MVVM Light Toolkit)方法

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

and constructor is

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会选择参数最多的构造函数,但是您没有指定应该如何解析这些参数。

你可以试试这个,看看它是否会造成MyAppServiceAgent的默认构造函数(paramaterless)时,这种类型的解决被称为..

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

我认为什么是更好的是去除来自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类。

相关问题