with azure brokeredmessage get the body without knowing the type

后端 未结 3 786
一个人的身影
一个人的身影 2021-01-30 14:03

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 =         


        
3条回答
  •  故里飘歌
    2021-01-30 14:33

    Here is the complete code to deserialize from the brokeredmessage:

    public T GetBody(BrokeredMessage brokeredMessage)
    {
      var ct = brokeredMessage.ContentType;
      Type bodyType = Type.GetType(ct, true);
    
      var stream = brokeredMessage.GetBody();
      DataContractSerializer serializer = new DataContractSerializer(bodyType);
      XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max);
      object deserializedBody = serializer.ReadObject(reader);
      T msgBase = (T)deserializedBody;
      return msgBase;
    }
    

提交回复
热议问题