Masstransit RabbitMQ发布者未能发布 "正确 "的合同。

问题描述 投票:0回答:1

我一直在使用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);

另一端(receiverubscriber API上的消费者)只能获得基元类型字段,例如。名称, 说明IsActive.

其余字段解析为 无效 尽管我清楚地知道,之前发布的属性的。配置文件更新 有正确的价值观。

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

是因为传承的原因吗?

亲切的问候。

编辑#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);
    }
}

这是我的发布者类

public class ProfilePublisher : IPublisher { private readonly IPublishEndpoint _publishEndPoint;

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

    public async Task PublishProfileUpdate(object profileUpdate)
    {
        await _publishEndPoint.Publish<Profile>(profileUpdate);
    }
}
asp.net-core properties rabbitmq asp.net-core-webapi masstransit
1个回答
0
投票

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

在与@Chris Patterson讨论后,我发现,在两个.NET核心Web API之间建立消息合约是非常重要的。

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

虽然我对造成这种行为的原因仍然感到十分困惑,但这一方法还是奏效了。

© www.soinside.com 2019 - 2024. All rights reserved.