private async Task <bool> CallHandlers(IHandlerRegistry handlers, Type type, object message, Action <object> action) { var result = true; var errorHandler = handlers.FindErrorHandler(type); var typeHandlers = handlers.FindHandler(type); if (typeHandlers != null) { foreach (var handler in typeHandlers) { var hresult = true; var errorMessage = "No error."; try { await handler(message, action); } catch (Exception ex) { errorMessage = string.Format("Message handler failed: {0}", ex.Message); _logger.Error("Message handler exception:", ex); hresult = false; } if (!hresult && errorHandler != null) { try { await errorHandler(errorMessage, action); } catch (Exception ex) { _logger.Error("Error handler exception:", ex); } } result = result && hresult; } } else { _logger.Error("Handler for type '{0}' not found. Dropping message.", type.Name); result = false; } return(result); }
public static IEnumerable <Func <object, Action <object>, Task> > FindHandler <T>(this IHandlerRegistry registry) { return(registry.FindHandler(typeof(T))); }