简体   繁体   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)

Note this is not a duplicate question of MassTransit messages types must not be System types exception .请注意,这不是MassTransit 消息类型不能是系统类型异常的重复问题。

I am using RabbitMQ version 8.0.2 in Asp.NET Core Web API (.Net 6) .我在Asp.NET Core Web API (.Net 6)中使用RabbitMQ 8.0.2 版。 I can publish a custom object successfully by using Publish method of IPublishEndpoint , however, whenever I try to send publish List of the object I get this error:我可以使用IPublishEndpointPublish方法成功发布自定义对象,但是,每当我尝试发送对象的发布列表时,我都会收到此错误:

System.ArgumentException: Messages types must not be System type

Here is the full sample:这是完整的示例:

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;
        }
    }

And in Program.csProgram.cs

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

Why I can't use IList with Publish methods?为什么我不能将IListPublish方法一起使用?

You can't use IList<T> with Publish because it isn't supported.您不能将IList<T>Publish一起使用,因为它不受支持。 There are some PublishBatch extension methods that enumerate the list and call Publish for each element.有一些PublishBatch扩展方法可以枚举列表并为每个元素调用Publish

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

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