0

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.

saurav.rox
  • 345
  • 1
  • 2
  • 12
  • 1
    What is the type of `_azServiceBusConsumer`? What's the SDK you are using? – Gaurav Mantri Aug 01 '23 at 09:11
  • @GauravMantri Thank you for the reply. Updated my question. could you please check now? – saurav.rox Aug 01 '23 at 09:18
  • 1
    Please edit your question and include the code for both `IAzServiceBusConsumer` and its concrete implementation. – Gaurav Mantri Aug 01 '23 at 09:23
  • @GauravMantri Added more info to my question. Thank you. – saurav.rox Aug 01 '23 at 09:27
  • They also asked for the concrete implementation... doesn't matter though, it looks like you're using the [deprecated SDK](https://stackoverflow.com/questions/73359526/replacing-microsoft-azure-servicebus-with-azure-messaging-servicebus), try using `Azure.Messaging.ServiceBus` instead as it is more intuitive – DisplayName Aug 01 '23 at 09:42

2 Answers2

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);
Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
1

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);