Service Reference使用Arrays而不是List <Type>,即使设置说使用List也是如此

Nat*_*han 9 c# web-services

我正在使用Visual Studio 2010,并且我已经获得了对我们创建的Web服务的服务引用.我们的方法返回包含通用List属性的对象:

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; }
  }
}
Run Code Online (Sandbox Code Playgroud)

在客户端,它使用int []而不是List创建一个References.cs文件:

[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;
Run Code Online (Sandbox Code Playgroud)

在服务引用设置上,"集合类型"设置为使用"列表",而不是"阵列".然而,它仍然这样做.

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

CkH*_*CkH 9

您是否添加了"服务参考"或"Web参考"?似乎代理是使用XmlSerializer而不是DataContractSerializer生成的.如果使用DataContractSerializer,您将拥有System.Runtime.Serialization ... Attributes而不是Xml.Serialization ...属性.您是如何生成此Web引用的?更新后的XmlSerializer会将所有集合转换为Arrays,而Datacontract序列化程序知道如何生成.Net DataTypes.添加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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

看看它如何为您服务并告诉我们.

  • VS2013 - 删除ServiceReference - >添加服务参考 - >高级 - >集合类型:= System.Collections.Generic.List. (2认同)

Nat*_*ate 5

在添加服务引用中,您可以选择用于集合的类型。出于某种原因,Array 是默认值。更改它后,我不得不删除整个引用并重新添加它,从一开始就选择 List。我在事后改变它时遇到了奇怪的问题。

  • 我已经做到了这一点,通过在它甚至可以创建引用本身之前设置 Collection 值。但我仍然得到数组。 (4认同)