Protobuf-net fails to deserialize Guid

早过忘川 提交于 2019-12-13 01:34:53

问题


Im hosting a WCF service on top of IIS 7.5 & using protobuf-net bindings.

I have a data contract with a Guid type property :


[Datacontract(Namespace = "http://lorem")]
public class MyCustomData {
  [DataMember(Order = 0)]
  public Guid Id { get; set; } // this ends up as empty, ie. 0000-0000-....
  [DataMember(Order = 1)]
  public int MyInt { get; set; } // serializes/deserializes ok
}

[ServiceContrac(Namespace = "http://ipsum"), Protobuf.ProtoContract]
public interface IMyService {
  ...
}

[ServiceBehavior(Namespace = "https://ipsum")]
public class MyService {
  public void ServiceMethod(MyCustomData data) {
    ...
  }
}

Issue is that MyCustomData.Id is empty when the call comes through to the service.


回答1:


Solved:

Issue was caused by "feature" of protobuf-net

Order 0 is discarded by protobuf-net (Does protobuf-net support [DataMember(Order=0)]?):


[Datacontract(Namespace = "http://lorem")]
public class MyCustomData {

  [DataMember(Order = 0)]
  public int Dummy { get; set; } // had to define, otherwise I get invalid wire-type exception

  [DataMember(Order = 1)]
  public Guid Id { get; set; } // now ok

  [DataMember(Order = 2)]
  public int MyInt { get; set; } // serializes/deserializes ok
}


来源:https://stackoverflow.com/questions/20376783/protobuf-net-fails-to-deserialize-guid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!