集合接口和WCF [英] Collection interfaces and WCF

查看:120
本文介绍了集合接口和WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和WCF做Web服务。我有一个实现IEnumerable类的成员变量。我想有它由做序列化为我的数据合同的一部分:

I'm using C# and WCF to do web services. I have a member variable of a class that implements IEnumerable. I tried to have it be serialized as part of my data contract by doing:

[DataContract]
class Item
{
    [DataMember]
    private IEnumerable<KeyValuePair<string, object>> Member1
    {
        get { return this._Properties; }
        set { this._Properties = Value; }
    }
}

在哪里_properties的类型是:

Where _Properties is of type:

class PropertiesCollection:IEnumerable<KeyValuePair<string, object>>
{
    ...
}

我认为,WCF将序列的member1作为一个IEnumerable,因为这是我给该成员的类型,但我得到一个InvalidDataContractEx​​ceptiom,此消息:

I thought that WCF would serialize Member1 as an IEnumerable, because that's the type I gave to that member, but I'm getting an InvalidDataContractExceptiom, with this message:

键入PropertyCollectioncannot序列化。考虑与DataContractAttribute属性将其标记和标记所有成员想要序列化的DataMemberAttribute属性。如果类型是一个集合,考虑与CollectionDataContractAttribute其标记。请参阅Microsoft .NET Framework文档等支持的类型。

Type PropertyCollectioncannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

看来WCF是看对象的运行时类型,通过member1中(PropertyCollection)被传递,而不是财产(IEnumenable)的类型。这是理解是否正确?是唯一的修复将[CollectionDataContract]到PropertiesCollection?我想preFER到keek服务合同的非特定的(也就是说,它只是知道物联网是一个IEnumerable),如果可能的话。

It seems WCF is looking at the runtime type of the object being passed via Member1 (PropertyCollection) rather than the type of the property (IEnumenable). Is this understanding correct? Is the only fix adding [CollectionDataContract] to PropertiesCollection? I would prefer to keek the service contract non-specific (i.e. it just knows that iot is an IEnumerable), if possible.

推荐答案

哦呵真正aswer对这个问题是有一些要求,一个类型必须满足是一个有效的集合DataContract:

Ehe real aswer to this question is that there are some requirements that a type must fulfill to be a valid collection DataContract:

  1. 在它必须有一个参数的构造函数
  2. 必须有一个添加方法。

当我实现了我的那些问题就走了。

When I implemented those my problem went away.

这篇关于集合接口和WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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