As far as I know, the default setting for "EasyNetQ" requires that the type of serialized object be consistent between applications. For example, you can send any known .NET type as String:
bus.Publish<String>("Excellent.");
and he will be happy in both projects.
You can use your own message if you put it in a shared library (dll). Since you specifically mentioned that they live in different projects, I would suggest serializing and pouring the objects yourself.
EasyNetQ uses interal Newtonsoft Json.NET to serialize such objects. As you can see, your message has already been serialized as:
Message: {"Text": "Hello World"}
To do this, you still need to add a link to Json.NET, because EasyNetQ hides this link using ilrepack .
This should work:
bus.Publish<string>(JsonConvert.SerializeObject(new MessageA { Text = "Hello World" }));
and
bus.Subscribe<string>("", HandleClusterNodes); private static void HandleClusterNodes(string obj) { var myMessage = (MessageB)JsonConvert.DeserializeObject<MessageB>(obj); Console.WriteLine(myMessage.Text); }
But you will lose your attribute-based routing, and you might want to change your methods.
If you want to use the basic methods, you can set the theme as follows:
bus.Publish<string>(JsonConvert.SerializeObject(new MessageA { Text = "Hello World" }), "topic.name"); bus.Subscribe<string>("", HandleClusterNodes, new Action<EasyNetQ.FluentConfiguration.ISubscriptionConfiguration>( o => o.WithTopic("topic.name")));
But in order to have a complete control, you need to use the Advanced API ;
var yourMessage = new Message<string>(JsonConvert.SerializeObject(new MessageA { Text = "Hello World" })); bus.Advanced.Publish<string>(new Exchange("YourExchangeName"), "your.routing.key", false, false, yourMessage);
and on the subscriber part:
IQueue yourQueue = bus.Advanced.QueueDeclare("AnotherTestMessagesQueue"); IExchange yourExchange = bus.Advanced.ExchangeDeclare("YourExchangeName", ExchangeType.Topic); bus.Advanced.Bind(yourExchange, yourQueue, "your.routing.key"); bus.Advanced.Consume<string>(yourQueue, (msg, info) => HandleClusterNodes(msg.Body));
which is almost the same as the original RabbitMQ C # API.
Detailed analysis:
The main problem is this exception:
EasyNetQ.EasyNetQException: cannot find type EasyNetQSample.ProgramA + MessageA: EasyNetQSample
This is called by EasyNetQ because it cannot find a special class on the endpoint.
If we look at the source code of TypeNameSerializer.cs , you will see
var type = Type.GetType(nameParts[0] + ", " + nameParts[1]); if (type == null) { throw new EasyNetQException( "Cannot find type {0}", typeName); }
here he tried to find the EasyNetQSample.ProgramA.Message A file in the second project, while he only knew EasyNetQSample.ProgramB.Message B.
Alternatively, you can deploy your own ISerializer or set ITypeNameSerializer to the default serializer , but I have not tried this.