When to use Observable.FromEventPattern rather than Observable.FromEvent?

baralong picture baralong · Jun 24, 2011 · Viewed 22.6k times · Source

We've got a client calling off to a TIBCO EMS queue and are wiring up the events like this:

var msgConsumer = _session.CreateConsumer(responseQueue);
var response = Observable.FromEvent<EMSMessageHandler,EMSMessageEventArgs>
            (h => msgConsumer.MessageHandler += h, h => msgConsumer.MessageHandler -= h)
            .Where(arg => arg.Message.CorrelationID == message.MessageID);

When I call response.Subscribe(...) I get System.ArgumentException "Error binding to target method."

I can make it work with Observable.FromEventPattern<EMSMessageEventArgs>(msgConsumer, "MessageHandler") but then it's got the event as a string and just not as clean.

Also I have IObservable<EventPattern<EMSMessageEventArgs>> rather than IObservable<EMSMessageEventArgs>

What I'd like to understand is: when should I use FromEvent over FromEventPattern? It seems a bit trial and error.

Answer

Richard Anthony Hein picture Richard Anthony Hein · Jun 24, 2011

"Use FromEvent for events structurally don't look like a .NET event pattern (i.e. not based on sender, event args), and use FromEventPattern for the pattern-based ones." - Bart De Smet (Rx team)