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.
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 :
// Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
MaxConcurrentCalls = 1,
AutoComplete = false
};
// Register the function that will process messages
queueClient.RegisterMessageHandler(await queueClient.CompleteAsync(message.SystemProperties.LockToken), messageHandlerOptions);
and after that your queueClient ready for call CloseAsync() liek
queueClient.CloseAsync().Wait();
and your sender should be something like
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
await queueClient.SendAsync(message);