You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MassTransit v8发送消息时Null值未序列化问题求助

问题:MassTransit 8.0.5 + Azure Service Bus 无法将null值序列化到消息中

我们使用MassTransit 8.0.5,以Azure Service Bus作为传输载体,为兼容其他传输方式仍采用Newtonsoft Json序列化器,需要在发送的消息中保留null值。但MassTransit v8默认的null值处理逻辑为NullValueHandling.Ignore,尝试以下两种配置后均未生效:

已尝试的解决方法

  • 在DTO类上添加特性:[JsonObject(ItemNullValueHandling = NullValueHandling.Include)](针对需要保留null的属性所在类)
  • 全局配置MassTransit的Newtonsoft序列化器:
services.AddMassTransit(x =>
{
    x.SetKebabCaseEndpointNameFormatter();
    x.UsingAzureServiceBus((_, cfg) =>
    {
        cfg.UseNewtonsoftJsonSerializer();
        cfg.ConfigureNewtonsoftJsonDeserializer(settings =>
        {
            settings.NullValueHandling = NullValueHandling.Include;
            return settings;
        });
        cfg.ConfigureNewtonsoftJsonSerializer(settings =>
        {
            settings.NullValueHandling = NullValueHandling.Include;
            return settings;
        });
        cfg.Host(massTransitConnectionString);
    });
});

当前结果与期望

当前发送到服务总线的JSON片段

"secondaryContacts": [
  {
    "contactType": "Unknown"
  },
  {
    "contactType": "Unknown"
  }
],

期望的JSON片段

"secondaryContacts": [
    {
      "contactType": "Unknown",
      "title": null,
      "firstName": null,
      "lastName": null,
      "address": null,
      "email": null,
      "mobilePhone": null,
      "preferredLanguageCode": null
    },
    {
      "contactType": "Unknown",
      "title": null,
      "firstName": null,
      "lastName": null,
      "address": null,
      "email": null,
      "mobilePhone": null,
      "preferredLanguageCode": null
    }
  ],

解决建议

  1. 精准配置DTO特性

    • 确保[JsonObject(ItemNullValueHandling = NullValueHandling.Include)]特性添加在secondaryContacts对应的子DTO类上,而非仅父类:
      [JsonObject(ItemNullValueHandling = NullValueHandling.Include)]
      public class SecondaryContact
      {
          public string ContactType { get; set; }
          public string Title { get; set; }
          // 其他属性...
      }
      
    • 若仅需保留特定属性的null值,可在单个属性上使用[JsonProperty(NullValueHandling = NullValueHandling.Include)]:
      public class SecondaryContact
      {
          public string ContactType { get; set; }
          [JsonProperty(NullValueHandling = NullValueHandling.Include)]
          public string Title { get; set; }
          // 其他属性同理配置
      }
      
  2. 调整MassTransit配置顺序
    序列化器配置需优先于主机配置,避免被覆盖:

    services.AddMassTransit(x =>
    {
        x.SetKebabCaseEndpointNameFormatter();
        x.UsingAzureServiceBus((context, cfg) =>
        {
            cfg.UseNewtonsoftJsonSerializer();
            // 先配置序列化器参数
            cfg.ConfigureNewtonsoftJsonSerializer(settings =>
            {
                settings.NullValueHandling = NullValueHandling.Include;
                return settings;
            });
            cfg.ConfigureNewtonsoftJsonDeserializer(settings =>
            {
                settings.NullValueHandling = NullValueHandling.Include;
                return settings;
            });
            // 最后配置主机
            cfg.Host(massTransitConnectionString);
        });
    });
    
  3. 排查序列化器覆盖问题
    确认项目中未通过其他方式(如AddJsonSerializer)覆盖Newtonsoft序列化器的配置,确保当前配置是最终生效的。

  4. 验证DTO实例属性值
    发送消息前,确认secondaryContacts中每个对象的title、firstName等属性确实被设置为null(引用类型默认null,但值类型需声明为可空类型)。

内容的提问来源于stack exchange,提问作者Marcel Niederhauser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 20:31:04