Events typically don't throw, so it's possibly the wrong behavior at the source. If you can fix it at the source, then do so.
Otherwise, you'll either have to catch and swallow the error:
a => this._topicSubscribers.ForEach(s =>
{
try
{
s.MessageHandler += a;
}
catch
{
}
})
which perhaps isn't ideal, or just don't use the FromEvent method:
return Observable.Create<EventPattern<EMSMessageEventArgs>>(observer =>
{
EMSMessageHandler handler = (sender, e) =>
observer.OnNext(new EventPattern<EMSMessageEventArgs>(sender, e)));
try
{
_topicSubscribers.ForEach(s => s.MessageHandler += handler);
}
catch (Exception ex)
{
try
{
_topicSubscribers.ForEach(s => s.MessageHandler -= handler);
}
catch { }
observer.OnError(ex);
}
return Disposable.Create(() =>
{
try
{
_topicSubscribers.ForEach(s => s.MessageHandler -= handler);
}
catch { }
});
});
Note that Rx requires serialized notifications (§4.2 in the Rx Design Guidelines) so you must ensure that all of the _topicSubscribers raise events sequentially, never concurrently. If you cannot, then you must synchronize all calls to observer.OnNext yourself, probably by acquiring a lock.
Update: To be clear, serialization is required regardless of whether you use FromEvent or Create, so even if you choose to simply swallow exceptions like my first example, you'll still need to make sure that the source never raises events concurrently; if you cannot, then you're forced to use my Create example with a lock anyway. FromEvent doesn't do this for you.