-
Notifications
You must be signed in to change notification settings - Fork 10
Non generic publish & subscription extension methods
AzureNetQ allows you to subscribe to, and publish a message simply by providing a handler for a given message type:
bus.Subscribe<MyMessage>(x => Console.WriteLine(x.Text));
and
bus.Publish<MyMessage>(theMessage);
But what do you do if you are discovering the message type at runtime? For example, you might have some system which loads add-ins and wants to subscribe to message types on their behalf. AzureNetQ provides you with non-generic publish and subscription methods just for this purpose.
The generic publish methods can be found as members of the IBus interface:
void Publish(Type type, object message);
void Publish(Type type, object message, string topic);
void Publish(Type type, object message, Action<IPublishConfiguration> configure);
void Publish(Type type, object message, string topic, Action<IPublishConfiguration> configure);
Task PublishAsync(Type type, object message);
Task PublishAsync(Type type, object message, string topic);
Task PublishAsync(Type type, object message, Action<IPublishConfiguration> configure);
Task PublishAsync(Type type, object message, string topicName, Action<IPublishConfiguration> configure);
In order to use the generic subscribe methods, just add this using statement:
using AzureNetQ.NonGeneric;
...which provides you with these subscription extension methods:
public static IDisposable Subscribe(this IBus bus, Type messageType, Action<object> onMessage)
public static IDisposable Subscribe(
this IBus bus,
Type messageType,
Action<object> onMessage,
Action<ISubscriptionConfiguration> configure)
public static IDisposable SubscribeAsync(this IBus bus, Type messageType, Func<object, Task> onMessage)
public static IDisposable SubscribeAsync(
this IBus bus,
Type messageType,
Func<object, Task> onMessage,
Action<ISubscriptionConfiguration> configure)
They are just like the Publish and Subscribe methods on IBus except that you provide a Type argument instead of the generic argument, and the message handler is an Action<object> instead of an Action<T>.
Here’s an example of the non-generic subscribe in use:
var messageType = typeof(MyMessage);
bus.Subscribe(messageType, x =>
{
var message = (MyMessage)x;
Console.Out.WriteLine("Got Message: {0}", x.Text);
});
Here's an example of the non-generic publish in use:
var messageType = typeof(MyMessage);
bus.Publish(messageType, theMessage);
- 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