private void OnProcessFailed(string messageId, DomainEventMessageContent content, Exception exception)
        {
            EventHandler <MessageProcessFailedEventArgs> eh = this.ProcessFailed;

            if (eh != null)
            {
                MessageProcessFailedEventArgs e = new MessageProcessFailedEventArgs(messageId, content, exception);
                eh(this, e);
            }
        }
        private void OnProcessing(DomainEventMessageContent content)
        {
            EventHandler <MessageProcessingEventArgs> eh = this.Processing;

            if (eh != null)
            {
                MessageProcessingEventArgs e = new MessageProcessingEventArgs(content);
                eh(this, e);
            }
        }
 public MessageProcessFailedEventArgs(string messageId, DomainEventMessageContent messageContent, Exception exception)
 {
     this.MessageId      = messageId;
     this.MessageContent = messageContent;
     this.Exception      = exception;
 }
 public MessageProcessingEventArgs(DomainEventMessageContent messageContent)
 {
     this.MessageContent = messageContent;
 }
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker localWorker = sender as BackgroundWorker;

            if (localWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            List <string> allMessageIds = e.Argument as List <string>;
            var           messageCount  = allMessageIds.Count;
            List <DomainEventMessageContent> messageContents = new List <DomainEventMessageContent>();

            using (MessageQueue messageQueue = new MessageQueue(this.EventMessageQueue))
            {
                messageQueue.MessageReadPropertyFilter.SentTime = true;
                for (int i = 0; i < messageCount; i++)
                {
                    Message message        = messageQueue.PeekById(allMessageIds[i], this.EventMessageReceiveTimeout);
                    var     messageContent = new DomainEventMessageContent(message);
                    messageContents.Add(messageContent);
                }
                messageQueue.Close();
            }
            var sortedMessageContents = messageContents.OrderBy(mc => mc.SentTime);

            foreach (var mc in sortedMessageContents)
            {
                bool canRemove = true;
                try
                {
                    if (!mc.IsValidMessage)
                    {
                        throw new Exception("Invalid Message Content.");
                    }
                    OnProcessing(mc);
                    Type eventType = Type.GetType(mc.Type);
                    if (eventType != null)
                    {
                        DomainEventXmlSerializer xmlSerializer = new DomainEventXmlSerializer();
                        var domainEvent = xmlSerializer.Deserialize(eventType, mc.Bytes);
                        messageDispatcher.DispatchMessage(domainEvent);
                    }
                    else
                    {
                        canRemove = false;
                    }
                }
                catch (Exception ex)
                {
                    OnProcessFailed(mc.MessageId, mc, ex);
                    canRemove = false;
                }
                finally
                {
                    if (canRemove)
                    {
                        using (MessageQueue messageQueue = new MessageQueue(this.EventMessageQueue))
                        {
                            try
                            {
                                messageQueue.ReceiveById(mc.MessageId, this.EventMessageReceiveTimeout);
                            }
                            finally
                            {
                                messageQueue.Close();
                            }
                        }
                    }
                }
            }
        }