skip to Main Content

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:

enter image description here

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


  1. from the official doc:

    Payload serialization When in transit or stored inside of Service Bus,
    the payload is always an opaque, binary block. The ContentType
    property enables applications to describe the payload, with the
    suggested format for the property values being a MIME content-type
    description according to IETF RFC2045; for example,
    application/json;charset=utf-8.

    ,,,

    If the payload of a message can’t be deserialized, then it’s
    recommended to dead-letter the message.

    Try to serialize the message using JSON format before sending it to the queue/topic:

                var messageBody = JsonSerializer.Serialize(invoiceMessage );
                //Set content type and Guid
                var message = new Message(Encoding.UTF8.GetBytes(messageBody)) {
                    MessageId = Guid.NewGuid().ToString(),
                        ContentType = "application/json"
                };
                await client.SendAsync(message);
    
    Login or Signup to reply.
  2. 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); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search