Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

EasyNetQ: Non-Generic Subscribe

DZone's Guide to

EasyNetQ: Non-Generic Subscribe

·
Free Resource

Since it’s very first version, EasyNetQ has allowed you to subscribe to a message simply by providing a handler for a given message type (and a subscription id, but that’s another discussion).

bus.Subscribe<MyMessage>("subscriptionId", x => Console.WriteLine(x.Text));

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. Before today (version 0.24) you would have had to employ some nasty reflection mojo to deal with this scenario, but now EasyNetQ provides you with non-generic subscription methods out-of-the-box.

Just add the this using statement:

using EasyNetQ.NonGeneric;

Which provides you with these extension methods:

public static IDisposable Subscribe(this IBus bus, Type messageType, string subscriptionId, Action<object> onMessage)
public static IDisposable Subscribe(
    this IBus bus, 
    Type messageType, 
    string subscriptionId, 
    Action<object> onMessage, 
    Action<ISubscriptionConfiguration> configure)
public static IDisposable SubscribeAsync(
    this IBus bus,
    Type messageType,
    string subscriptionId,
    Func<object, Task> onMessage)
public static IDisposable SubscribeAsync(
    this IBus bus, 
    Type messageType, 
    string subscriptionId, 
    Func<object, Task> onMessage, 
    Action<ISubscriptionConfiguration> configure)

They are just like the 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, "my_subscriptionId", x =>
    {
        var message = (MyMessage)x;
        Console.Out.WriteLine("Got Message: {0}", x.Text);
    });
Very useful I think, and one of the most commonly asked for features.
Happy runtime discovery!
Topics:

Published at DZone with permission of Mike Hadlow, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}