Csharp/C#教程:Service Reference使用Arrays而不是List ,即使设置说使用List也是如此分享


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

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

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

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

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

在服务引用设置上,“集合类型”设置为使用“列表”,而不是“arrays”。 然而,它仍在这样做。

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

您是否添加了“服务参考”或“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 intValues; [DataMember] public System.Collections.Generic.List IntValues { get { return intValues; } set { intValues = value; } } } 

看看它如何为您服务并让我们知道。

在“添加服务引用”中,您可以选择要用于集合的类型。 由于某种原因,Array是默认的。 更改后,我必须删除整个引用并重新添加它,从头开始选择List。 事实上,我有一些奇怪的问题在改变它。

上述就是C#学习教程:Service Reference使用Arrays而不是List ,即使设置说使用List也是如此分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/950677.html

(0)
上一篇 2021年11月17日
下一篇 2021年11月17日

精彩推荐