【问题标题】:Masstransit RabbitMQ publisher fails to publish "correct" contractMasstransit RabbitMQ 发布者未能发布“正确”合约
【发布时间】:2020-06-23 09:25:46
【问题描述】:

一段时间以来,我一直在使用 Masstransit 和 RabbitMQ 创建消息契约以实现两个 .NET 核心 Web API 之间的通信,我偶然发现了以下内容。

假设我在两个项目中都有以下指定的合同:

public class Profile : BaseEntity
{
    public string Name { get; set; }

    public string Description { get; set; }

    public bool IsActive { get; set; }

    public DefaultMode Mode { get; set; }

    public string SuperMode { get; set; }

    public ExtraClient? Client { get; set; }
}

每当我发布消息时

await _publishEndPoint.Publish<Profile>(profileUpdate);

另一端(接收者/订阅者 API 上的消费者)只能获取原始类型字段,例如 NameDescriptionIsActive。

其余字段被解析为 null,尽管我很清楚之前发布了 profileUpdate 有合适的值。

有没有人遇到过类似的情况?

是不是因为继承?

亲切的问候,

编辑#1:语法

编辑#2:发布更多信息

这里要求的是我的消费类:

public class ProfileUpdateConsumer : IConsumer<Profile>
{
    private readonly IProfilesHub _profileHub;

    public ProfileUpdateConsumer(IProfilesHub profileHub)
    {
        _profileHub = profileHub;
    }

    public async Task Consume(ConsumeContext<Profile> context)
    {
        await _profileHub.BroadcastProfileUpdate(context.Message);
    }
}

这是我的发布者类:

公共类 ProfilePublisher : IPublisher { 私有只读 IPublishEndpoint _publishEndPoint;

    public ProfilePublisher(IPublishEndpoint publishEndpoint)
    {
        _publishEndPoint = publishEndpoint;
    }

    public async Task PublishProfileUpdate(object profileUpdate)
    {
        await _publishEndPoint.Publish<Profile>(profileUpdate);
    }
}

【问题讨论】:

  • 您是否共享了所有可供发布者以及消费者使用的类?
  • @AlexeyZimarev 是的,消费者和发布者共享相同的类,但为了澄清,它是一个共享项目而不是共享类库。你认为这可能会影响它吗?
  • 如果您可以发布您正在发布的课程,以及未能获得这些字段的消费者课程,那将会有所帮助。我的猜测是您正在发布基本类型 Profile ,因此子类消息类型不可用。但同样,代码在这里说话。
  • @ChrisPatterson 我已经更新了更多代码,够不够?
  • 为什么反对? public async Task PublishProfileUpdate(object profileUpdate) - 这应该是 PublishProfileUpdate(T update) where T : Profile - 虽然为什么不直接发布 T?

标签: asp.net-core properties rabbitmq asp.net-core-webapi masstransit


【解决方案1】:

我知道这已经有一段时间了,但对于任何可能遇到这种情况的人来说。

在与@Chris Patterson 讨论后,

我手动将一种数据类型转换为另一种数据类型,并确保所有字段都存在。

这很有效,尽管我仍然对导致这种行为的原因感到很困惑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多