[英]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 版。 我可以使用IPublishEndpoint
的Publish
方法成功發布自定義對象,但是,每當我嘗試發送對象的發布列表時,我都會收到此錯誤:
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");
});
});
});
為什么我不能將IList
與Publish
方法一起使用?
您不能將IList<T>
與Publish
一起使用,因為它不受支持。 有一些PublishBatch
擴展方法可以枚舉列表並為每個元素調用Publish
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.