代码之家  ›  专栏  ›  技术社区  ›  Nathan

服务引用使用数组而不是List<Type>,即使设置说使用List

  •  9
  • Nathan  · 技术社区  · 14 年前

    我使用的是visualstudio2010,我有一个对我们创建的web服务的服务引用。我们的方法返回包含泛型列表属性的对象:

    public class ExampleResponse
    {
      private System.Collections.Generic.List<int> intValues;
    
      [WCF::MessageBodyMember(Name = "IntValues")]
      public System.Collections.Generic.List<int> IntValues    
      {
        get { return intValues; }
        set { intValues= value; }
      }
    }
    

    在客户端,它创建了一个References.cs文件,其中int[]不是List:

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace="SomeNamespace", Order=0)]
    [System.Xml.Serialization.XmlArrayAttribute(IsNullable=true)]
    [System.Xml.Serialization.XmlArrayItemAttribute(Namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays", IsNullable=false)]
    public int[] IntValues;
    

    在服务引用设置中,集合类型设置为使用列表,而不是数组。然而,它仍在这样做。

    任何关于如何解决这个问题的信息都会非常有用,似乎毫无意义。

    2 回复  |  直到 14 年前
        1
  •  9
  •   CkH    14 年前

    您添加了“服务引用”还是“Web引用”?代理似乎是使用XmlSerializer而不是DataContractSerializer生成的。如果使用了DataContractSerializer,则会有System.Runtime.Serialization。。。属性而不是Xml.Serialization。。。属性。您究竟是如何生成此web引用的?更新后的XmlSerializer将所有集合转换为数组,其中Datacontract序列化程序知道如何生成.Net数据类型。添加Web引用使用XmlSerializer BTW。

    另外,我很好奇你对MessageBodyMember的使用。为什么要尝试生成自己的MessageContracts。弄乱MessageContracts是非常危险的,尤其是当你不知道自己在做什么的时候。

    相反,请尝试以下操作:

    [DataContract]
    public class ExampleResponse
    {
        private System.Collections.Generic.List<int> intValues;
    
        [DataMember]
        public System.Collections.Generic.List<int> IntValues
        {
            get { return intValues; }
            set { intValues = value; }
        }
    }
    

    看看这对你有什么好处,然后告诉我们。

        2
  •  3
  •   Nate CSS Guy    14 年前

    在Add Service引用中,可以选择要用于集合的类型。由于某些原因,数组是默认的。更改后,我不得不删除整个引用并重新添加,从一开始就选择List。我有奇怪的问题在事后改变它。