簡體   English   中英

如何從Python中的Azure服務總線消息讀取C#對象

[英]How to read C# object from Azure service bus message in Python

我目前正在嘗試根據從azure服務總線獲得的消息確定消息內容。 但是,內容是我懷疑是用C#編寫的對象。

<uamqp.c_uamqp.cMessage object at 0x00000000033B3810>

我當前正在使用python,我想訪問該對象並獲取其屬性。 有什么辦法嗎?

編輯:這只是一個消息對象中包含的屬性

{'auto_renew_error': None, 
'_encoding': 'UTF-8', 
'_expiry': None, 
'received_timestamp': datetime.datetime(2019, 8, 8, 13, 21, 39,
405000), 
'_receiver': <azure.servicebus.receive_handler.Receiver object at 0x00000000033A47F0>, 
'_annotations': {'x-opt-enqueued-time': 1565284860716L, 'x-opt-enqueue-sequence-number': 0L, 'x-opt-locked-until': 1565284959471L, 'x-opt-sequence-number': 3406L},
'header': <uamqp.message.MessageHeader object at 0x00000000039BE400>, 
'_app_properties': None, 
'message': <uamqp.message.Message object at 0x00000000039BE320>, 
'properties': <uamqp.message.MessageProperties object at 0x00000000039BE2E8>}
def getAttribute(message):
        print(message.__dict__['message'].__dict__['_message'])

上面的代碼基本上只接收消息對象,然后讀取其屬性,並繼續查看嵌入在初始消息對象中的對象的屬性。 我被困在'_messages'對象上,因為我無法使用讀取其屬性。 字典

即使該對象是用C#創建的,也應正確序列化並讀取沒有問題。 您需要解碼主體並解析JSON消息。

while True:
    msg = bus_service.receive_subscription_message(config.topic, config.subscription, peek_lock=True)

    if msg.body is None:
        print('No messages')
        continue

     print(json.loads(msg.body.decode("utf-8")))

您能否提供更多代碼來說明您的方法以及在何處獲得該結果?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM