-->

Azure service bus python client issue

2020-07-22 04:52发布

问题:

I am using azure service bus for the first time to transfer message between C# source and python client.

I am creating ServiceBusService object and using it i fetch message from function receive_subscription_message.

The payload i get is not as expected.

My code is something like below.

bus_service = ServiceBusService(service_namespace="service_namespace", shared_access_key_name="All", shared_access_key_value="password")
msg = bus_service.receive_subscription_message('test', 'test', peek_lock=True)
print(msg.body)
msg.delete()

msg.body gives a byte string like below: b'@\x06string\x083http://schemas.microsoft.com/2003/10/Serialization/\x9ae\x0c{"PUID":"3NFLzV3cjCp8f5JLh3KSnkXDgSw1FWgM","HardDelete":null}\x01'

Originally json was pushed in it. Is there any way to avoid additional parameters and fetch original json only?

回答1:

The issue was caused by the different protocol used at the different side: sender in C# using AMQP or default .NET serialization behavior and receiver in Python using REST API via HTTP. You can refer to the document Using Service Bus from .NET with AMQP 1.0 and the source code of Azure Python Service Bus SDK to know that, and I had explained the similar issue in the other SO thread Interoperability Azure Service Bus Message Queue Messages.

To avoid additional parameters and fetch original json only, there are two ways which you can try, as below.

  1. Using REST API via HTTP in C# to send original json without any additional content from AMQP or default .NET serialization behavior。
  2. Using python-qpid-proton package in Python to receiver message with AMQP to deserialize and get the original json.
  3. Under the current case, just remove the addition content from the messages to extract the origin json, due to there is the same addition content per message.

Hope it helps.