Azure service bus - how to add metadata to the message

こ雲淡風輕ζ 提交于 2020-05-24 03:29:25

问题


I am using .net core web app as the publisher and .net core console app as subscriber. I am able to successfully pass messages between these two systems using Managed Identities - set up in Azure portal.

My question is I need to add metadata to the the message that is being sent. How do I do that ?

Below is my publisher code :

string data = JsonConvert.SerializeObject(payloadEvents);
Message message = new Message(Encoding.UTF8.GetBytes(data));

var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();

TopicClient sendClient = new TopicClient(_serviceBusNamespace, _topicName, tokenProvider, retryPolicy: null);

await sendClient.SendAsync(message);


回答1:


Message object has a property called UserProperties that can be used to set custom metadata for that message.

Something like:

string data = JsonConvert.SerializeObject(payloadEvents);
Message message = new Message(Encoding.UTF8.GetBytes(data));
message.UserProperties.Add("key1", "value1");
message.UserProperties.Add("key2", "value2");

var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();

TopicClient sendClient = new TopicClient(_serviceBusNamespace, _topicName, tokenProvider, retryPolicy: null);

await sendClient.SendAsync(message);


来源:https://stackoverflow.com/questions/61628422/azure-service-bus-how-to-add-metadata-to-the-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!