与天蓝色的经纪人消息不知道类型的身体 [英] with azure brokeredmessage get the body without knowing the type
本文介绍了与天蓝色的经纪人消息不知道类型的身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Azure Service Bus中使用代理消息时,可以通过调用.GetBody检索消息的正文.代码很简单:
When you are using the brokered message in the Azure Service Bus, you can retrieve the body of the message with the call .GetBody. The code is simple:
var msg = subscription.Receive();
MyPayload payload = msg.GetBody<MyPayload>();
但是,有一种方法可以在不显式了解body对象类的情况下检索Body吗?
However, is there a way to retrieve the Body without explicitly knowing the class of the body object?
var msg = subscription.Receive();
Type bodyType = Type.GetType( msg.ContentType);
var payload = msg.GetBody<bodyType>();
推荐答案
以下是从代理消息反序列化的完整代码:
Here is the complete code to deserialize from the brokeredmessage:
public T GetBody<T>(BrokeredMessage brokeredMessage)
{
var ct = brokeredMessage.ContentType;
Type bodyType = Type.GetType(ct, true);
var stream = brokeredMessage.GetBody<Stream>();
DataContractSerializer serializer = new DataContractSerializer(bodyType);
XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max);
object deserializedBody = serializer.ReadObject(reader);
T msgBase = (T)deserializedBody;
return msgBase;
}
这篇关于与天蓝色的经纪人消息不知道类型的身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文