1

下面是在运行 protobuf-net 版本 2.0.0.480 的 WCF 环境中工作和不工作的类定义。

知道处理代码使用实际类型填充响应列表,在本例中为 SomeClassRow。

更奇怪的是,对于糟糕的 SomeResponse 版本,WCF 有时有效,有时无效。

这就是我使用 IDataRow 接口的原因。这让我的响应列表可以保存任何类型的行。

所以问题是,当列表是用强类型与接口 (IDataRow) 定义时,为什么 protobuf 无法反序列化?

Thanks,
Ninos

[ProtoContract]
[ProtoInclude(101, typeof(BaseClassInfo))]
public interface IDataRow
{
}

[ProtoContract]
[ProtoInclude(101, typeof(SomeClassRow))]
public class BaseClassInfo : IDataRow
{
   [ProtoMember(1)]
   public int SomeId { get; set; }
   ...
}

// This version of the class won't work because it defines the collection with the
// strongly type name i.e. List<Some
[ProtoContract]
public class SomeResponse
{
   [ProtoMember(1)]
   public List<SomeClassRow> Rows { get; set; }

   ...
}

// SomeClassRow is in turn an IDataRow.
[ProtoContract]
public class SomeClassRow : BaseClassInfo
{
   [ProtoMember(1)]
   public string Name { get; set; }

   [ProtoMember(2)]
   public int Value { get; set; }

   ...  
}

// But, if the response is defined as follows then the WCF deserializes correcty and ALWAYS.
[ProtoContract]
public class SomeResponse
{
   [ProtoMember(1)]
   public List<IDataRow> Rows { get; set; }

   ...
}
4

0 回答 0