var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); // <------错误点 Console.WriteLine(" [x] Received {0}", message); };
var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body.ToArray(); // 将内存区域的内容复制到一个新的数组中 var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received {0}", message); };
② Span
1 2 3 4 5 6 7
var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body.Span; // 从内存区域获取一个跨度 var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received {0}", message); };