我正在尝试接收从具有标头但没有正文的服务总线发送的消息。这些消息不是使用 MassTransit 发送的,但我想通过 MassTransit 接收。
我在接收端收到此错误:
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.
我无法想象如果它没有身体的话这也是可能的。
这就是我建立公共交通的方式
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);
});
});
这就是我的消费者的样子
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;
}
}
MassTransit 需要消息正文,您需要创建自己的反序列化器,以允许使用由消费者处理的消息类型来使用空消息正文。您无法使用来自使用者的内部 Azure 服务总线消息类型。