在公共交通中发布事件时,某些消息属性为空

Some message properties are null while publishing event in mass transit

我有一个普通的 .Net web API 项目,为了学习事件驱动架构,我开始了一个小项目。我是公共交通的新手,我已经使用他们的文档编写了代码。我对命令使用 bus.Send(command),对事件使用 bus.Publish(event)。当我发送命令时,我在命令处理程序中收到所有预期值,但在发布时某些属性为空。当我出于某种原因发布消息时,Id 和 EntityThatTookAction 为空,但填充了其他属性。我不明白这种行为的原因。我错过了什么吗?非常感谢有关此问题的帮助

配置

services.AddMassTransit(massTransitConfig =>
{
    massTransitConfig.AddConsumer<UserEventConsumer>();
    massTransitConfig.AddConsumer<UserCommandHandler>();
    massTransitConfig.AddConsumer<TodoListEventConsumer>();
    massTransitConfig.AddConsumer<TodoListCommandHandler>();

    massTransitConfig.UsingRabbitMq((context, rabbitMqConfig) =>
    {
        rabbitMqConfig.Host(new Uri(busConfig["Url"]), h =>
        {
            h.Username(busConfig["Username"]);
            h.Password(busConfig["Password"]);
        });
        
        rabbitMqConfig.ReceiveEndpoint(nameof(UserEventConsumer), cfg =>
        {
            cfg.PurgeOnStartup = true;
            cfg.PrefetchCount = 10;
            cfg.Bind<UserCreatedEvent>();
            cfg.Bind<UserUpdatedEvent>();
            cfg.Bind<UserDeletedEvent>();
            cfg.ConfigureConsumer(context, typeof(UserEventConsumer));
        });
        
        rabbitMqConfig.ReceiveEndpoint(nameof(UserCommandHandler), cfg =>
        {
            cfg.PurgeOnStartup = true;
            cfg.PrefetchCount = 10;
            cfg.Bind<CreateUserCommand>();
            cfg.Bind<UpdateUserCommand>();
            cfg.Bind<DeleteUserCommand>();
            cfg.ConfigureConsumer(context, typeof(UserCommandHandler));
            EndpointConvention.Map<CreateUserCommand>(cfg.InputAddress);
            EndpointConvention.Map<UpdateUserCommand>(cfg.InputAddress);
            EndpointConvention.Map<DeleteUserCommand>(cfg.InputAddress);
        });
        
        rabbitMqConfig.ReceiveEndpoint(nameof(TodoListEventConsumer), cfg =>
        {
            cfg.PurgeOnStartup = true;
            cfg.PrefetchCount = 10;

            cfg.ConfigureConsumer(context, typeof(TodoListEventConsumer));
        });
        
        rabbitMqConfig.ReceiveEndpoint(nameof(TodoListCommandHandler), cfg =>
        {
            cfg.PurgeOnStartup = true;
            cfg.PrefetchCount = 10;
            
            cfg.ConfigureConsumer(context, typeof(TodoListCommandHandler));
            EndpointConvention.Map<CreateTodoListCommand>(cfg.InputAddress);
            EndpointConvention.Map<UpdateTodoListCommand>(cfg.InputAddress);
            EndpointConvention.Map<DeleteTodoListCommand>(cfg.InputAddress);
        });
    });
});

活动

public class UserCreatedEvent : IBaseEvent
{
    public string Id { get; }
    public string Message { get; }
    public string EntityThatTookAction { get; }
    public DateTimeOffset CreatedOn { get; }
    public EntityType EntityType { get; }
    public Guid CorrelationId { get; }

    public UserCreatedEvent(string entityId, string entity, EntityType entityType, Guid correlationId, DateTimeOffset createdOn)
    {
        Id = entityId;
        EntityThatTookAction = entity;
        CreatedOn = createdOn;
        EntityType = entityType;
        CorrelationId = correlationId;
        Message = "User list created";
    }
}

界面

public interface IBaseEvent : CorrelatedBy<Guid>
{
    public string Id { get; }
    public string Message { get;}
    public string EntityThatTookAction { get;}
    public DateTimeOffset CreatedOn { get;}
    public EntityType EntityType { get; }
}

发布

var message = new UserCreatedEvent(entityId, entity, EntityType.User, correlationId, DateTimeOffset.UtcNow);
await _bus.Publish(message);

您没有 post 您的命令合同有效,但我会寻找两者之间的任何差异,看看可能发生什么。可能是您的属性上没有 { get; private set; },但我不记得是否需要这样做。我认为需要对其进行设置才能使反序列化工作。如果您使用的是 .NET 5,则可以改为指定 { get; init; }

此外,我清理了你的总线配置,你可能真的不想使用 PurgeOnStartupEndpointConvention

services.AddMassTransit(massTransitConfig =>
{
    massTransitConfig.AddConsumer<UserEventConsumer>();
    massTransitConfig.AddConsumer<UserCommandHandler>();
    massTransitConfig.AddConsumer<TodoListEventConsumer>();
    massTransitConfig.AddConsumer<TodoListCommandHandler>();

    massTransitConfig.UsingRabbitMq((context, rabbitMqConfig) =>
    {
        rabbitMqConfig.Host(new Uri(busConfig["Url"]), h =>
        {
            h.Username(busConfig["Username"]);
            h.Password(busConfig["Password"]);
        });

        rabbitMqConfig.ReceiveEndpoint(nameof(UserEventConsumer), cfg =>
        {
            cfg.PrefetchCount = 10;
            cfg.ConfigureConsumer<UserEventConsumer>(context);
        });

        rabbitMqConfig.ReceiveEndpoint(nameof(UserCommandHandler), cfg =>
        {
            cfg.PrefetchCount = 10;
            cfg.ConfigureConsumer<UserCommandHandler>(context);
        });

        rabbitMqConfig.ReceiveEndpoint(nameof(TodoListEventConsumer), cfg =>
        {
            cfg.PrefetchCount = 10;
            cfg.ConfigureConsumer<TodoListEventConsumer>(context);
        });

        rabbitMqConfig.ReceiveEndpoint(nameof(TodoListCommandHandler), cfg =>
        {
            cfg.PrefetchCount = 10;
            cfg.ConfigureConsumer<TodoListCommandHandler>(context);
        });
    });
});