服务参考使用数组而不是 List<Type>,即使设置说使用 List [英] Service Reference is using Arrays instead of List&lt;Type&gt;, even when settings say to use List

查看:20
本文介绍了服务参考使用数组而不是 List<Type>,即使设置说使用 List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Visual Studio 2010,我有一个对我们创建的 Web 服务的服务引用.我们的方法返回包含通用 List 属性的对象:

I'm using Visual Studio 2010, and I've got a service reference to a web service we created. Our methods return objects that contain generic List properties:

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

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

On the client-side, it creates a References.cs file with int[] instead of 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;

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

On the service reference settings the Collection Type is set to use List, not Arrays. Yet, it's still doing so.

任何有关如何解决此问题的信息都会非常有帮助,但似乎毫无意义.

Any info on how to solve this would be extremely helpful, it seems to make no sense.

推荐答案

您添加了服务引用"还是Web 引用"?看起来代理是使用 XmlSerializer 而不是 DataContractSerializer 生成的.如果使用 DataContractSerializer,您将拥有 System.Runtime.Serialization... 属性而不是 Xml.Serialization... 属性.您究竟是如何生成此 Web 参考的?更新后的 XmlSerializer 会将所有集合转换为数组,而 Datacontract 序列化器知道如何生成 .Net 数据类型.添加 Web 引用使用 XmlSerializer BTW.

Did you add a "Service Reference" or a "Web Reference"? It appears that the proxy was generated with the XmlSerializer instead of the DataContractSerializer. If the DataContractSerializer was used, you would have System.Runtime.Serialization... Attributes instead of the Xml.Serialization... attributes. How exactly did you generate this web reference? The updated XmlSerializer will convert all collections to Arrays, where as, the Datacontract serializer knows how to generate .Net DataTypes. Add Web Reference uses the XmlSerializer BTW.

另外,我很好奇您对 MessageBodyMember 的使用.为什么要尝试生成自己的 MessageContracts.使用 MessageContracts 可能非常危险,尤其是当您不确切知道自己在做什么时.

Also, I'm curious about your use of MessageBodyMember. Why are you trying to generate your own MessageContracts. Messing with MessageContracts can be very dangerous, especially if you don't know exactly what you are doing.

相反,请尝试以下操作:

Instead, try the following:

[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; }
    }
}

看看这对您有何帮助,然后告诉我们.

See how that works for you and let us know.

这篇关于服务参考使用数组而不是 List<Type>,即使设置说使用 List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆