Search code examples
c#jsonazureservicebusmasstransit.net-8.0

Receive message on MassTransit with empty body from Azure Service Bus


I'm trying to receive messages sent from a service bus that has a header but not a body. The messages aren't sent using MassTransit but I want to receive with MassTransit.

I'm getting this error on the receiving end:

System.Runtime.Serialization.SerializationException: An error occured while deserializing the message envelope
       ---> System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.

I cannot figure out it's possible if it doen't have body.

this is how I set up mass transit

serviceCollection.AddMassTransit(x =>
{
    x.AddConsumers(typeof(MessageConsumer).Assembly);
    x.UsingAzureServiceBus((context, cfg) =>
    {
        cfg.Host(configuration.GetValue<string>(connectionString));
        cfg.SubscriptionEndpoint("subscriptionName", "topicPath", endpointCfg =>
        {
            endpointCfg.Consumer<MessageConsumer>(context);
        });

        cfg.ConfigureEndpoints(context);
    });
});

This is how my consumer looks

public class MessageConsumer : IConsumer<ServiceBusReceivedMessage>
{
    private readonly ILogger<MessageConsumer> _logger;

    public MessageConsumer(ILogger<MessageConsumer> logger)
    {
        _logger = logger;
    }

    public Task Consume(ConsumeContext<ServiceBusReceivedMessage> context)
    {
        _logger.LogInformation("Message id: {MessageId}", context.MessageId);
        return Task.CompletedTask;
    }
}

Solution

  • MassTransit requires a message body, you'd need to create your own deserializer that allows for an empty message body to be consumed with a message type handled by the consumer. You can't consume the internal Azure Service Bus message types from a consumer.