繁体   English   中英

无法通过 MassTransit 中 IPublishEndpoint 的 Publish 方法发送自定义对象列表(消息类型不得为系统类型)

[英]Could not send list of custom objects via Publish method of IPublishEndpoint in MassTransit (Messages types must not be System type)

请注意,这不是MassTransit 消息类型不能是系统类型异常的重复问题。

我在Asp.NET Core Web API (.Net 6)中使用RabbitMQ 8.0.2 版。 我可以使用IPublishEndpointPublish方法成功发布自定义对象,但是,每当我尝试发送对象的发布列表时,我都会收到此错误:

System.ArgumentException: Messages types must not be System type

这是完整的示例:

public class WeatherForecastController : ControllerBase
    {
        private readonly IPublishEndpoint _publishEndpoint;
        public WeatherForecastController(IPublishEndpoint publishEndpoint)
        {
            _publishEndpoint = publishEndpoint;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public async Task<IEnumerable<WeatherForecast>> Get()
        {
            var data = Enumerable.Range(1, 3).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                myDictionary = new Dictionary<string, string>
                                {
                                    { "key1", "value1" },
                                    { "key2", "value2" }
                                }
            }).ToList();

            //Error!
            await _publishEndpoint.Publish<IList<WeatherForecast>>(data);

            //Working 
           //await _publishEndpoint.Publish<WeatherForecast>(data.FirstOrDefault());
            
            return data;
        }
    }

Program.cs

builder.Services.AddMassTransit(options => {
    options.UsingRabbitMq((context, cfg) =>
    {
        cfg.Host(new Uri("rabbitmq://localhost:5672"), h =>
        {
            h.Username("guest");
            h.Password("guest");
        });
    });
});

为什么我不能将IListPublish方法一起使用?

您不能将IList<T>Publish一起使用,因为它不受支持。 有一些PublishBatch扩展方法可以枚举列表并为每个元素调用Publish

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM