Refactor This!
The following method is ugly, what kind of refactoring would you do here?
private void HandleMessageCompletion( Message message, TransactionScope tx, OpenedQueue messageQueue, Exception exception, Action<CurrentMessageInformation, Exception> messageCompleted, Action<CurrentMessageInformation> beforeTransactionCommit) { var txDisposed = false; if (exception == null) { try { if (tx != null) { if (beforeTransactionCommit!=null) beforeTransactionCommit(currentMessageInformation); tx.Complete(); tx.Dispose(); txDisposed = true; } try { if (messageCompleted != null) messageCompleted(currentMessageInformation, exception); } catch (Exception e) { logger.Error("An error occured when raising the MessageCompleted event, the error will NOT affect the message processing", e); } return; } catch (Exception e) { logger.Warn("Failed to complete transaction, moving to error mode", e); exception = e; } } try { if (txDisposed == false && tx != null) { logger.Warn("Disposing transaction in error mode"); tx.Dispose(); } } catch (Exception e) { logger.Warn("Failed to dispose of transaction in error mode.", e); } if (message == null) return; try { if (messageCompleted != null) messageCompleted(currentMessageInformation, exception); } catch (Exception e) { logger.Error("An error occured when raising the MessageCompleted event, the error will NOT affect the message processing", e); } try { var copy = MessageProcessingFailure; if (copy != null) copy(currentMessageInformation, exception); } catch (Exception moduleException) { logger.Error("Module failed to process message failure: " + exception.Message, moduleException); } if (messageQueue.IsTransactional == false)// put the item back in the queue { messageQueue.Send(message); } }
Note, please use pastebin.com/ instead of posting code in the comments.

Comments
Comment preview