-
Notifications
You must be signed in to change notification settings - Fork 10
Send receive
Whereas the Publish/Subscribe and Request/Response patterns are location transparent, in that you don't need to specify where the consumer of the message is located, the Send/Receive pattern is specifically designed for communication via a named queue. It also makes no assumptions about the types of message that can be sent via the queue. This means that you can send different types of message via the same queue.
AzureNetQ uses Service Bus Queues as it's underlying transport mechanism for Send/Receive.
The send/receive pattern is ideal for creating 'command pipelines', where you want a buffered channel to a single command processor.
To send a message, use the Send method on IBus, specifying the name of the queue you wish to sent the message to and the message itself:
bus.Send("my.queue", new MyMessage{ Text = "Hello Widgets!" });
To setup a message receiver for a particular message type, use the Receive method on IBus:
bus.Receive<MyMessage>("my.queue", message => Console.WriteLine("MyMessage: {0}", message.Text));
You can set up multiple receivers for different message types on the same queue by using the Receive overload that takes an Action<IReceiveRegistration>, for example:
bus.Receive("my.queue", x => x
.Add<MyMessage>(message => deliveredMyMessage = message)
.Add<MyOtherMessage>(message => deliveredMyOtherMessage = message));
If a message arrives on a receive queue that doesn't have a matching receiver, AzureNetQ will throw an exception saying 'No handler found for message type <message type>'.
Note: You probably do not want to call bus.Receive more than once for the same queue. This will create a new consumer on the queue and Service Bus will alternate between them. If you are consuming different types on different Receive calls (and thus different consumers), some of your messages will throw errors because AzureNetQ will not find a handler for your message type associated with the consumer on which it is consumed.
- Quick Start
- Introduction
- Casing in point: Topics and topics, Subscriptions and subscriptions
- Installing AzureNetQ
- Connecting to Service Bus
- Logging
- Publish
- Subscribe
- Request Response
- Send Receive
- Topic Based Routing
- Controlling Queue names
- Polymorphic Publish and Subscribe
- Scheduling Events with Future Publish
- Auto Subscriber
- Non Generic Publish & Subscription Extension Methods
- Replacing AzureNetQ Components
- Using a DI Container