嗨,我正在尝试通过 EasyNetQ 通过 RabbitMQ 发送一个简单的对象。我在订阅端反序列化该对象时遇到问题。任何人都可以向我展示这是如何工作的样本。请记住,要发送的对象是在它自己的项目中定义的,而不是在发布者和订阅者之间共享。这是我的示例,也许您可以告诉我它有什么问题?
方案一:
class ProgramA
{
static void Main(string[] args)
{
using (var bus = RabbitHutch.CreateBus("host=localhost"))
{
Console.WriteLine("Press any key to send the message");
Console.ReadKey();
bus.Publish(new MessageA { Text = "Hello World" });
Console.WriteLine("Press any key to quit");
Console.ReadKey();
}
}
public class MessageA
{
public string Text { get; set; }
}
}
方案 B:
class ProgramB
{
static void Main(string[] args)
{
using (var bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Subscribe<MessageB>("", HandleClusterNodes);
Console.WriteLine("Press any key to quit");
Console.ReadKey();
}
}
private static void HandleClusterNodes(MessageB obj)
{
Console.WriteLine(obj.Text);
}
[Queue("TestMessagesQueue", ExchangeName = "EasyNetQSample.ProgramA+MessageA:EasyNetQSample")]
public class MessageB
{
public string Text { get; set; }
}
}
这是我收到的错误:
DEBUG: HandleBasicDeliver on consumer: f9ded52d-039c-411a-9b9f-5c8ee3301854, deliveryTag: 1
DEBUG: Received
RoutingKey: ''
CorrelationId: 'ec41faea-a0c8-4ffd-8163-2cbf85d45fcd'
ConsumerTag: 'f9ded52d-039c-411a-9b9f-5c8ee3301854'
DeliveryTag: 1
Redelivered: False
ERROR: Exception thrown by subscription callback.
Exchange: 'EasyNetQSample.ProgramA+MessageA:EasyNetQSample'
Routing Key: ''
Redelivered: 'False'
Message:
{"Text":"Hello World"}
BasicProperties:
ContentType=NULL, ContentEncoding=NULL, Headers=[], DeliveryMode=2, Priority=0, CorrelationId=ec41faea-a0c8-4ffd-8163-2cbf85d45fcd, ReplyTo=NULL, Expiration=NULL, MessageId=NULL, Timestamp=0, Type=EasyNetQSample.ProgramA+MessageA:EasyNetQSample, UserId=NULL, AppId=NULL, ClusterId=NULL
Exception:
System.AggregateException: One or more errors occurred. ---> EasyNetQ.EasyNetQException: Cannot find type EasyNetQSample.ProgramA+MessageA:EasyNetQSample
at EasyNetQ.TypeNameSerializer.DeSerialize(String typeName)
at EasyNetQ.DefaultMessageSerializationStrategy.DeserializeMessage(MessageProperties properties, Byte[] body)
at EasyNetQ.RabbitAdvancedBus.<>c__DisplayClass19.<Consume>b__18(Byte[] body, MessageProperties properties, MessageReceivedInfo messageReceivedInfo)
at EasyNetQ.RabbitAdvancedBus.<>c__DisplayClass1e.<Consume>b__1d(Byte[] body, MessageProperties properties, MessageReceivedInfo receviedInfo)
at EasyNetQ.Consumer.HandlerRunner.InvokeUserMessageHandler(ConsumerExecutionContext context)
--- End of inner exception stack trace ---
---> (Inner Exception #0) EasyNetQ.EasyNetQException: Cannot find type EasyNetQSample.ProgramA+MessageA:EasyNetQSample
at EasyNetQ.TypeNameSerializer.DeSerialize(String typeName)
at EasyNetQ.DefaultMessageSerializationStrategy.DeserializeMessage(MessageProperties properties, Byte[] body)
at EasyNetQ.RabbitAdvancedBus.<>c__DisplayClass19.<Consume>b__18(Byte[] body, MessageProperties properties, MessageReceivedInfo messageReceivedInfo)
at EasyNetQ.RabbitAdvancedBus.<>c__DisplayClass1e.<Consume>b__1d(Byte[] body, MessageProperties properties, MessageReceivedInfo receviedInfo)
at EasyNetQ.Consumer.HandlerRunner.InvokeUserMessageHandler(ConsumerExecutionContext context)<---
我需要做什么才能正确反序列化MessageA
?