Protobuf.net WCF 反序列化列表<T> [英] Protobuf.net WCF Deserialize List<T>
问题描述
我尝试将 WCF 与 protobuf-net r.282 一起使用
I try use WCF with protobuf-net r.282
好的.我用 ProtoBehavior 属性标记我的合同
Ok. I mark my contracts with ProtoBehavior attribute
[OperationContract,ProtoBehavior]
[FaultContract(typeof(ServiceFaultException))]
Dictionary<ActivityCategoryDTO, SalesTemplateDTO> GetSalesTemplates();
[OperationContract, ProtoBehavior]
[FaultContract(typeof(ServiceFaultException))]
List<ActivityCategoryDTO> GetActivities();
接下来,- DTO:
[DataContract]
[Serializable]
[ProtoContract]
public class ActivityCategoryDTO
{
[DataMember]
[ProtoMember(1)]
public int Id { get; set; }
[DataMember]
[ProtoMember(2)]
public string Guid { get; set; }
[DataMember]
[ProtoMember(3)]
public string Name { get; set; }
}
我尝试从客户端使用此服务.当我调用 GetSalesTemplates 时 - 一切正常.我已经成功地反序列化了字典,但是当我调用 GetActivities 时,我在客户端得到了 null.通过fiddler我看到,数据传输成功,所以我认为是解串器问题.
I try consume this servise from client. When I call GetSalesTemplates - all is OK. I've got successful deserialized dictionary, but when I call GetActivities I get null at client. Through fiddler I see that, data is transmitting succesfully, so I think it's deserializer problem.
怎么了?如何在客户端获取列表?
What's wrong? How can I get List at Client?
编辑
似乎我对所有列表都有问题:)
It seems that I have problems with all Lists :)
[DataContract]
[Serializable]
[ProtoContract]
public class SalesTemplateDTO
{
[ProtoMember(1)]
[DataMember]
public string Name { get; set; }
[ProtoMember(2)]
public List<FieldTemplateDTO> Fields;
}
客户端只用名称,字段列表又为空.虽然所有的数据也是传输的.
It comes to client just with Name, List of Fields is null again. Though all data is transmitted too.
推荐答案
OK;我已经复制了,这实际上看起来是 IDE/svcutil 拒绝重新使用来自 DTO 程序集的服务合同(接口),即使它正在重新使用数据合同(类).mex 生成的服务合同(接口)缺乏必要的额外属性,因此 protobuf-net 永远不会被要求反序列化.
OK; I've reproduced and this actually looks to be the IDE/svcutil refusing to re-use the service-contract (interface) from your DTO assembly, even though it is re-using the data-contracts (classes). The mex-generated service-contract (interface) lacks the necessary extra attributes, so protobuf-net never gets asked to deserialize.
两个选项:
使用配置而不是代码定义protobuf用法;这样做的好处是无需对客户端或服务器进行任何更改即可使用.
use configuration rather than code to define the protobuf usage; the advantage of this is that it can be used without any changes to either client or server.
不要使用生成的服务包装器——一个通用的对应物可以简单地写成:
don't use the generated service wrapper - a generic counterpart can be written simply as:
public class Client<T> : ClientBase<T> where T : class
{
public T Service { get { return Channel; } }
public Client() {
}
public Client(string endpointConfigurationName) :
base(endpointConfigurationName) {
}
public Client(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
}
然后消费为:
using (var svc = new Client<IService1>())
{
var data = svc.Service.GetActivities();
}
这篇关于Protobuf.net WCF 反序列化列表<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!