C# 服务引用使用数组而不是列表<;类型>;,即使设置说要使用列表
我使用的是VisualStudio2010,我有一个对我们创建的web服务的服务引用。我们的方法返回包含泛型列表属性的对象:C# 服务引用使用数组而不是列表<;类型>;,即使设置说要使用列表,c#,web-services,C#,Web Services,我使用的是VisualStudio2010,我有一个对我们创建的web服务的服务引用。我们的方法返回包含泛型列表属性的对象: public class ExampleResponse { private System.Collections.Generic.List<int> intValues; [WCF::MessageBodyMember(Name = "IntValues")] public System.Collections.Generic.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; }
}
}
在服务引用设置中,集合类型设置为使用列表,而不是数组。然而,它仍在这样做
有关如何解决此问题的任何信息都将非常有用,似乎毫无意义。在“添加服务”参考中,您可以选择用于收集的类型。由于某些原因,数组是默认的。更改后,我不得不删除整个引用并重新添加,从一开始就选择List。我在事后更改它时遇到了一些奇怪的问题。您是否添加了“服务引用”或“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; }
}
}
[DataContract]
公共类示例响应
{
private System.Collections.Generic.List intValues;
[数据成员]
public System.Collections.Generic.List IntValues
{
获取{return intValues;}
设置{intValues=value;}
}
}
看看这对你有什么好处,让我们知道 我已经做到了这一点,在集合本身创建引用之前设置集合值。但我仍然得到数组。引用是通过右键单击项目并选择“添加服务引用…”创建的。当出现查找web服务的对话框时,我没有选择底部的web引用选项。我会检查一下你的另一个建议,但这个建议直到最近才奏效——它来自一个我们“继承”的web服务。好的,明白了。你的评论使我走上了一条我以前从未想过的道路。我们有一个枚举,它没有DataContract属性,所以VS2010在没有告诉我的情况下创建了一个WebReference。我也理解你所说的处理我们自己的邮件合同,这很难看。很高兴我能帮上忙。Happy CodingVS2013-删除服务引用-->添加服务引用-->高级-->收集类型:=System.Collections.Generic.List。
[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; }
}
}