Reputation: 163
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);
Upvotes: 2
Views: 2526
Reputation: 2266
UserProperties
were renamed to ApplicationProperties
in 7.x
Here's how I do it for sending/receiving on a queue, with a function queue trigger.
Send with metadata:
var client = new ServiceBusClient(connectionString);
var sender = client.CreateSender(queueName);
var serviceBusMessage = new ServiceBusMessage(body);
serviceBusMessage.ApplicationProperties["key1"] = "value1";
serviceBusMessage.ApplicationProperties["key2"] = "value2";
await sender.SendMessageAsync(serviceBusMessage);
Receive metadata:
[FunctionName(MyQueueReceiver)]
public async Task Run([ServiceBusTrigger(MyQueueName, Connection = ConnectionString)] ServiceBusReceivedMessage message, ILogger log)
{
var key1 = message.ApplicationProperties["key1"] as string;
// Do something with message.Body
}
Versions:
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.17.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.13.4" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.2.0" />
Note ServiceBusReceivedMessage
only exists in Microsoft.Azure.WebJobs.Extensions.ServiceBus
5.x, and currently 4.x is installed by default in a new function project (at least for me).
Upvotes: 0
Reputation: 136226
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);
Upvotes: 2