While preparing a presentation I created a simple demo app where I wanted to publish a message to a queue.
My first (naïve) attempt looked like this:
This failed with the following error message:
A convention for the message type {typename} was not found
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bus = Bus.Factory.CreateUsingRabbitMq(sbc => | |
{ | |
var host = sbc.Host(new Uri("rabbitmq://localhost:32778"), h => | |
{ | |
h.Username("guest"); | |
h.Password("guest"); | |
}); | |
}); | |
bus.Send(new ProcessSampleCommand() { Id = Guid.NewGuid() }); |
What was my mistake?
In MassTransit you have to make a difference between sending a message and publishing a message. Whereas publishing a message doesn’t require any extra configuration, sending a message does. Why? Because when sending a message you directly target a queue. MassTransit has to know which queue the message should be send to.
We have 2 options to make this work:
1) You specify the target queue using a convention:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EndpointConvention.Map<ProcessSampleCommand>(new Uri("rabbitmq://localhost:32778/sample_queue")); |
2) You use the ISendEndpointprovider to specify a queue:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var endpoint = await hub.GetSendEndpoint("rabbitmq://localhost:32778/sample_queue"); | |
await endpoint.Send(new ProcessSampleCommand()); |