[英]Why is MassTransit not serializing my incoming message in its consumer correctly?
我正在做一個項目,其中( .NET 6
)我使用AWS SQS/SNS來處理隊列和主題。 我有一個將消息發布到隊列的API
應用程序,以及一個監聽隊列並使用消息的控制台應用程序(服務)。
這是我在API
中的配置,它不消耗任何東西,只發布消息:
services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
x.UsingAmazonSqs((context, cfg) =>
{
cfg.Host("some-region", h =>
{
h.AccessKey(config.AccessKey);
h.SecretKey(config.AccessSecret);
h.Config(new AmazonSQSConfig() { RegionEndpoint = someregion });
});
cfg.ConfigureEndpoints(context);
});
});
這是我在控制台應用程序(服務)中與消費者的配置:
services.AddMassTransit(x =>
{
x.SetKebabCaseEndpointNameFormatter();
x.AddConsumer<CreateFeedbackCommandConsumer>();
x.AddCommands();
x.UsingAmazonSqs((context, cfg) =>
{
cfg.Host("some-region", h =>
{
h.AccessKey(sqsConfiguration.AccessKey);
h.SecretKey(sqsConfiguration.AccessSecret);
h.Config(new AmazonSQSConfig() { RegionEndpoint = someregion });
});
cfg.ReceiveEndpoint("create-feedback-command", endpoint =>
{
endpoint.ConfigureConsumeTopology = false;
endpoint.ClearSerialization();
endpoint.UseRawJsonSerializer();
endpoint.ConfigureConsumer<CreateFeedbackCommandConsumer>(context);
});
cfg.ConfigureEndpoints(context);
});
});
一切似乎 go 好,SQS/SNS 中的消息似乎具有正確的屬性和值,但是當我的消費者使用消息時,所有屬性都是空的或NULL 。 這是我第一次將MassTransit與SQS結合使用,在我使用Azure Service Bus時沒有出現這個問題。
消費者:
public class CreateFeedbackCommandConsumer : IConsumer<CreateFeedbackCommand>
{
private readonly ILogger<CreateFeedbackCommandConsumer> _logger;
public CreateFeedbackCommandConsumer (ILogger<CreateFeedbackCommandConsumer> logger)
{
_logger = logger;
}
public async Task Consume(ConsumeContext<CreateFeedbackCommand> context)
{
try
{
var command = context.Message;
_logger.LogInformation($"Incoming {nameof(CreateFeedbackCommand)}. CorrelationId: '{command.CorrelationId}'. DateTime (UTC): '{DateTime.UtcNow:dd-MMM-yyy hh:mm:ss}'");
if (command.Feedback == null)
{
throw new ArgumentNullException(nameof(command.Feedback));
}
_logger.LogInformation($"Finished {nameof(CreateFeedbackCommand)}. CorrelationId: '{command.CorrelationId}'. DateTime (UTC): '{DateTime.UtcNow:dd-MMM-yyy hh:mm:ss}'");
}
catch (Exception ex)
{
_logger.LogError(ex, $"An error has occurred while processing the {nameof(CreateFeedbackCommand)} command");
throw;
}
}
該命令如下所示:
public class CreateFeedbackCommand
{
public CreateFeedbackCommand(CreateFeedback feedback)
{
CorrelationId = Guid.NewGuid();
Feedback = feedback;
}
/// <summary>
/// Unique Correlation ID.
/// </summary>
public Guid CorrelationId { get; set; }
/// <summary>
/// Feedback.
/// </summary>
public CreateFeedback Feedback { get; set; }
}
總結一下:發布消息/命令不會導致錯誤/錯誤,消費者消費消息,但是context.Message
的屬性是NULL
或空。 例如, Feedback
屬性是 NULL。 當我查看SQS/SNS中的整個消息時,一切都是正確的。 所以我的猜測是,反序列化在消費者中出錯了。 我該如何解決?
你為什么要在消費者中這樣做?
endpoint.ClearSerialization();
endpoint.UseRawJsonSerializer();
您應該使用 MassTransit 中內置的默認 JSON 序列化和消息信封,除非您有非常具體的原因要更改它。
“因為”不是一個理由,說真的。 用它。
此外,由於您在生產者中沒有做同樣的事情,因此您正在使用消息信封序列化 JSON,但由於您已清除它,因此無法反序列化它。 確保將生產者和消費者配置為相同。
此外,如果您要從 API 發布消息,但設置ConfigureConsumeTopology = false
(這會禁用消費者的所有發布綁定),您希望如何將發布的消息放入隊列?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.