I am using the Azure Message Bus to communicate with my microservices. I am able to send the message under the topic Account Updated from service A. However, the message is not being read by another service called B. I mean if I send "Hello" string, it is being read properly. But the serialized message is not being decoded at the listener.
Here is the message that is sent from service A:
var invoiceMessage = new InvoiceMessageDto<AccountInvoicingMessageDto>
{
MessageType = MessageType.Create,
InvoiceType = InvoiceType.Account,
CreationDateTime = DateTime.Now,
MessageDto = new AccountInvoicingMessageDto
{
AccountId = request.Id,
OrganizationName = request.OrganizationName,
AccountStatus = AccountStatusEnum.ApprovedPendingPayment,
FlatFee = flatfee
}
};
try
{
Console.WriteLine("Success Success when sending Account Message");
_logger.LogInformation($"Success when sending Account Update Message");
_azServiceBusConsumer.SendMessage(invoiceMessage, MessageTopics.ACCOUNT_UPDATED);
}
catch (Exception ex)
{
_logger.LogInformation($"Error when sending Message: {ex}");
}
The _azServiceBusConsumer
is an instance of the interface IAzServiceBusConsumer
as shown below:
namespace Services.Common.Interface
{
public interface IAzServiceBusConsumer
{
void Start();
void Stop();
void SendMessage(IntegrationBaseMessage message, string? topicName);
}
}
And here is how I am listening to it in service B:
_receiverClient is defined as:
_receiverClient = new SubscriptionClient(_config["MessageBus:ConnectionString"],
MessageTopics.ACCOUNT_UPDATED,
_config["MessageBus:SubscriptionName"],
ReceiveMode.PeekLock,
RetryPolicy.Default);
And then it is used by the listener as:
_receiverClient.RegisterMessageHandler(async (message, cancelToken) =>
{
var bodyBytes = message.Body;
var ourMessage = Encoding.UTF8.GetString(bodyBytes);
_logger.LogInformation($"Received: {ourMessage}");
}
When checked in the debug mode, the message
variable shows the message that is received but bodyBytes
shows nothing when the mouse is hovered there.
So, how should I decode the message received by my listener (service B)?
Any help would be more than appreciable.
2
Answers
from the official doc:
Try to serialize the message using JSON format before sending it to the queue/topic:
The RegisterMessageHandler method of queue is used to register the function that will process the message.
This method expects two parameters: handler and message handler option.
something like :
and after that your queueClient ready for call CloseAsync() liek
and your sender should be something like