public void Receive(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout) { if (_disposed) { throw new ObjectDisposedException(_disposedMessage); } string successfulMessageId = null; try { Exception failedMessageException = null; _transport.Receive(acceptContext => { failedMessageException = null; if (successfulMessageId != null) { _log.DebugFormat("Received Successfully: {0}", successfulMessageId); _tracker.MessageWasReceivedSuccessfully(successfulMessageId); successfulMessageId = null; } Exception retryException; string acceptMessageId = acceptContext.OriginalMessageId ?? acceptContext.MessageId; if (_tracker.IsRetryLimitExceeded(acceptMessageId, out retryException)) { if (_log.IsErrorEnabled) { _log.ErrorFormat("Message retry limit exceeded {0}:{1}", Address, acceptMessageId); } failedMessageException = retryException; return(MoveMessageToErrorTransport); } if (acceptContext.MessageId != acceptMessageId) { if (_log.IsErrorEnabled) { _log.DebugFormat("Message {0} original message id {1}", acceptContext.MessageId, acceptContext.OriginalMessageId); } } Action <IReceiveContext> receive; try { acceptContext.SetEndpoint(this); _serializer.Deserialize(acceptContext); receive = receiver(acceptContext); if (receive == null) { Address.LogSkipped(acceptMessageId); _tracker.IncrementRetryCount(acceptMessageId, null); return(null); } } catch (SerializationException sex) { if (_log.IsErrorEnabled) { _log.Error("Unrecognized message " + Address + ":" + acceptMessageId, sex); } _tracker.IncrementRetryCount(acceptMessageId, sex); return(MoveMessageToErrorTransport); } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error("An exception was thrown preparing the message consumers", ex); } if (_tracker.IncrementRetryCount(acceptMessageId, ex)) { acceptContext.PublishPendingFaults(); } return(null); } return(receiveContext => { string receiveMessageId = receiveContext.OriginalMessageId ?? receiveContext.MessageId; try { receive(receiveContext); successfulMessageId = receiveMessageId; } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error("An exception was thrown by a message consumer", ex); } if (_tracker.IncrementRetryCount(receiveMessageId, ex)) { receiveContext.PublishPendingFaults(); } if (!receiveContext.IsTransactional) { SaveMessageToInboundTransport(receiveContext); } throw; } }); }, timeout); if (failedMessageException != null) { if (_log.IsErrorEnabled) { _log.ErrorFormat("Throwing Original Exception: {0}", failedMessageException.GetType()); } throw failedMessageException; } } catch (Exception ex) { if (successfulMessageId != null) { _log.DebugFormat("Increment Retry Count: {0}", successfulMessageId); _tracker.IncrementRetryCount(successfulMessageId, ex); successfulMessageId = null; } throw; } finally { if (successfulMessageId != null) { _log.DebugFormat("Received Successfully: {0}", successfulMessageId); _tracker.MessageWasReceivedSuccessfully(successfulMessageId); successfulMessageId = null; } } }
public void Receive(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout) { if (_disposed) { throw new ObjectDisposedException(_disposedMessage); } string successfulMessageId = null; try { Exception failedMessageException = null; _transport.Receive(acceptContext => { failedMessageException = null; if (successfulMessageId != null) { _log.DebugFormat("Received Successfully: {0}", successfulMessageId); _tracker.MessageWasReceivedSuccessfully(successfulMessageId); successfulMessageId = null; } Exception retryException; string acceptMessageId = acceptContext.OriginalMessageId ?? acceptContext.MessageId; IEnumerable <Action> faultActions; if (_tracker.IsRetryLimitExceeded(acceptMessageId, out retryException, out faultActions)) { if (_log.IsErrorEnabled) { _log.ErrorFormat("Message retry limit exceeded {0}:{1}", Address, acceptMessageId); } failedMessageException = retryException; acceptContext.ExecuteFaultActions(faultActions); return(MoveMessageToErrorTransport); } if (acceptContext.MessageId != acceptMessageId) { if (_log.IsErrorEnabled) { _log.DebugFormat("Message {0} original message id {1}", acceptContext.MessageId, acceptContext.OriginalMessageId); } } Action <IReceiveContext> receive; try { acceptContext.SetEndpoint(this); IMessageSerializer serializer; if (!_supportedSerializers.TryGetSerializer(acceptContext.ContentType, out serializer)) { throw new SerializationException( string.Format("The content type could not be deserialized: {0}", acceptContext.ContentType)); } serializer.Deserialize(acceptContext); receive = receiver(acceptContext); if (receive == null) { Address.LogSkipped(acceptMessageId); _tracker.IncrementRetryCount(acceptMessageId); return(null); } } catch (SerializationException sex) { if (_log.IsErrorEnabled) { _log.Error("Unrecognized message " + Address + ":" + acceptMessageId, sex); } _tracker.IncrementRetryCount(acceptMessageId, sex); return(MoveMessageToErrorTransport); } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error("An exception was thrown preparing the message consumers", ex); } if (_tracker.IncrementRetryCount(acceptMessageId, ex)) { acceptContext.ExecuteFaultActions(acceptContext.GetFaultActions()); } return(null); } return(receiveContext => { string receiveMessageId = receiveContext.OriginalMessageId ?? receiveContext.MessageId; try { receive(receiveContext); successfulMessageId = receiveMessageId; } catch (Exception ex) { if (_log.IsErrorEnabled) { _log.Error("An exception was thrown by a message consumer", ex); } faultActions = receiveContext.GetFaultActions(); if (_tracker.IncrementRetryCount(receiveMessageId, ex, faultActions)) { // seems like this might be unnecessary if we are going to reprocess the message receiveContext.ExecuteFaultActions(faultActions); } if (!receiveContext.IsTransactional) { SaveMessageToInboundTransport(receiveContext); } throw; } }); }, timeout); if (failedMessageException != null) { if (_log.IsErrorEnabled) { _log.ErrorFormat("Throwing Original Exception: {0}", failedMessageException.GetType()); } throw failedMessageException; } } catch (Exception ex) { if (successfulMessageId != null) { _log.DebugFormat("Increment Retry Count: {0}", successfulMessageId); _tracker.IncrementRetryCount(successfulMessageId, ex); successfulMessageId = null; } throw; } finally { if (successfulMessageId != null) { _log.DebugFormat("Received Successfully: {0}", successfulMessageId); _tracker.MessageWasReceivedSuccessfully(successfulMessageId); successfulMessageId = null; } } }