1
我有問題反序列化guid時定義的消息基類。當我收到以下消息時。protobuf網Guid反序列化Guid.Empty當使用繼承
[DataContract]
public abstract class GatewayPageEvent:IDomainEvent
{
protected GatewayPageEvent()
{
On = DateTime.Now;
}
[DataMember(Order = 1)] public Guid GatewayPageId { get; set; }
[DataMember(Order = 2)] public DateTime On { get; set; }
}
[DataContract]
public class GatewayPageAddedToSite : GatewayPageEvent
{
[DataMember(Order = 3)]public string Url { get; set; }
[DataMember(Order = 4)]public string SiteCode { get; set; }
}
GatewayPageId總是反序列化爲Guid.Empty。這是我寫的失敗的單元測試。我有點難住...
public string Serialize(object t)
{
var memoryStream = new MemoryStream();
ProtoBuf.Serializer.Serialize(memoryStream, t);
return Convert.ToBase64String(memoryStream.ToArray());
}
public object Deserialize(string value, Type targetType)
{
var bytes = Convert.FromBase64String(value);
var stream = new MemoryStream(bytes);
return ProtoBuf.Serializer.NonGeneric.Deserialize(targetType, stream);
}
[Test]
public void protobuf_serialization_can_deserialized_guids()
{
var originalMessage = new GatewayPageAddedToSite
{GatewayPageId = Guid.NewGuid(), SiteCode = "dls", Url = "test"};
var serializedMessage = Serialize(originalMessage);
var @event = (GatewayPageAddedToSite)Deserialize(serializedMessage, typeof(GatewayPageAddedToSite));
Assert.AreEqual(@event.GatewayPageId, originalMessage.GatewayPageId);
}
我知道Guids工作正常,因爲下面的單元測試工作得很好。
[Test]
public void guids_work_fine()
{
var original = Guid.NewGuid();
var serialized = Serialize(original);
var deserialized = (Guid) Deserialize(serialized, typeof (Guid));
Assert.AreEqual(original,deserialized);
}
我是否定義我的合同有誤?或者這是protobuf-net的問題嗎?
嘗試一下,但如果是這樣的話,不會是 Assert.AreEqual(originalMessage.On.Date,@ event.On.Date); 會失敗,但事實並非如此。 –
@Elliott 2秒,而我啓動了一個測試平臺... –
@Elliot似乎爲我解決了......? –