简体   繁体   English

如何从Python中的Azure服务总线消息读取C#对象

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

I am currently trying to determine the message contents from a message that I get from the azure service bus. 我目前正在尝试根据从azure服务总线获得的消息确定消息内容。 However, the content is an object which I suspect is written in C#. 但是,内容是我怀疑是用C#编写的对象。

<uamqp.c_uamqp.cMessage object at 0x00000000033B3810>

I am currently using python and I would like to access this object and obtain its attributes. 我当前正在使用python,我想访问该对象并获取其属性。 Is there any way about doing this? 有什么办法吗?

EDIT: This is just the attributes that is contained in one message object 编辑:这只是一个消息对象中包含的属性

{'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'])

Code above basically just takes message object, then read its attributes, and keeps on viewing attributes of objects which are embedded in the initial message object. 上面的代码基本上只接收消息对象,然后读取其属性,并继续查看嵌入在初始消息对象中的对象的属性。 I am stuck on the '_messages' object as I cannot read its attributes with . 我被困在'_messages'对象上,因为我无法使用读取其属性。 dict 字典

Even if the object is created in C# it should still be serialized properly and read without issues. 即使该对象是用C#创建的,也应正确序列化并读取没有问题。 You'll need to decode the body and parse the JSON message. 您需要解码主体并解析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")))

Can you provide more code showing your approach and where you are getting that result? 您能否提供更多代码来说明您的方法以及在何处获得该结果?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM