簡體   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