0

I am subscribed to an Azure Service Bus Topic and I am trying to retrieve objects sent from my Python client. But in my receiving end all I get is something like this:

<__main__.User object at 0x02F694F0>
<models.AssetPayload object at 0x038EA930>

I have tried receiving in python and in .NET. Here is a dummy code of what I have tried:

class User(object):
    def __init__(self, user_id, name):
        self.user_id = user_id
        self.name = name


user = User('123456', 'Shaphil')

# Send Message to 'myTopic'
msg = Message(bytes(user))
bus_service.send_topic_message('myTopic', msg)

# Receive Messages
msg = bus_service.receive_subscription_message('myTopic', 'AllMessages', peek_lock=False)
print msg.body

Receiving dummy code in C#:

var message = subscriptionClient.Receive();
var json = new StreamReader(message.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();

Console.WriteLine(json);

How can I retrieve the User object (the user_id and name) that was sent by the sender?

4

2 回答 2

0

A simple solution would be to call bytes() on the .__dict__ member of that instance. That is a standard Python dictand if your class is simple it will be JSON like serializable to "{'user_id': '123456', 'name': 'Shaphil'}".

Please try the following code snippet to create the Message object:

msg = Message(bytes(user.__dict__))

Any further concern, please feel free to let me know.

于 2016-06-22T05:50:06.163 回答
0

This should work.

message = Message(json.dumps(<dictionary to be converted into bytes>).encode('utf-8'))
于 2020-10-28T13:03:17.750 回答