因构造函数而解析类型时出错?

时间:2011-12-14 14:15:12

标签: singleton unity-container mvvm-light

我收到错误:

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>());
            }
        }

和构造函数是

 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 个答案:

答案 0 :(得分:1)

当您注册类型时,您没有指定在MyAppServiceAgent上调用哪个构造函数。默认情况下,Unity将选择具有最多参数的构造函数,但您没有指定应如何解析这些参数。

您可以尝试这个,看看是否会导致在解决此类型时调用MyAppServiceAgent的默认构造函数(无参数)。

_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类。