public IFatpipeMessage GetSuspendedMessage(string messageIdentifier)
        {
            string                 location         = "FpmDal->GetSuspendedMessage";
            IFatpipeMessage        result           = null;
            SuspendedMessageEntity suspendedMessage = null;

            try
            {
                Stopwatch watch = Stopwatch.StartNew();

                suspendedMessage =
                    (from e in this.suspendedTableServiceContext.CreateQuery <SuspendedMessageEntity>(MaargConfiguration.Instance[MaargConfiguration.SuspendedMessageTableName])
                     where e.RowKey == messageIdentifier
                     select e).FirstOrDefault();

                if (suspendedMessage != null)
                {
                    IFatpipeMessage message = null;
                    CloudBlob       blob    = this.suspendedContainer.GetBlobReference(suspendedMessage.SuspendedMessageBlobReference);
                    using (MemoryStream stream = new MemoryStream())
                    {
                        blob.DownloadToStream(stream);
                        stream.Position = 0;
                        message         = BizpipeMesssageStreaming.CreateBizpipeMessageFromStream(stream, this.fpm);
                    }

                    if (!string.IsNullOrEmpty(suspendedMessage.PartnerIdentifier))
                    {
                        IOutboundFatpipeMessage outboundMessage = this.fpm.CreateNewOutboundMessage();
                        outboundMessage.Header                    = message.Header;
                        outboundMessage.Body                      = message.Body;
                        outboundMessage.Status                    = message.Status;
                        outboundMessage.RoutingInfo               = new RoutingInfo();
                        outboundMessage.RoutingInfo.PartnerId     = suspendedMessage.PartnerIdentifier;
                        outboundMessage.RoutingInfo.TransportType = TransportType.Suspend;

                        result = outboundMessage;
                    }
                    else
                    {
                        result = message;
                    }
                }

                watch.Stop();
                LoggerFactory.Logger.Debug(location,
                                           "Message {0} retrieved from suspended queue in {1} ms.",
                                           messageIdentifier,
                                           watch.ElapsedMilliseconds);
            }
            catch (Exception exception)
            {
                LoggerFactory.Logger.Error(location, EventId.BizpipeGetSuspendMessage
                                           , "Error getting the suspended message {0}: {1}."
                                           , messageIdentifier
                                           , exception.ToString());
            }

            return(result);
        }
        public bool SaveSuspendedMessage(IFatpipeMessage message)
        {
            string location = "FpmDal->SaveSuspendedMessage";
            bool   result   = true;
            SuspendedMessageEntity suspendedMessage = null;

            //TODO remove the return statement
            // don't want any message to be suspended
            return(true);

            try
            {
                Stopwatch watch = Stopwatch.StartNew();
                suspendedMessage           = new SuspendedMessageEntity(message.Header.TenantIdentifier, message.Header.Identifier);
                suspendedMessage.Timestamp = DateTime.UtcNow;
                suspendedMessage.SuspendedMessageBlobReference = message.Header.Identifier;
                if (message.Status != null)
                {
                    suspendedMessage.ErrorMessage = string.Format(CultureInfo.InvariantCulture,
                                                                  "{0}. Message suspended after {1} retries. Error: {2}",
                                                                  message.Status.ProcessingResult,
                                                                  message.Status.NumberOfRetryAttempts,
                                                                  message.Status.ErrorDescription);
                }
                IOutboundFatpipeMessage outboundMessage = message as IOutboundFatpipeMessage;
                if (outboundMessage != null)
                {
                    if (outboundMessage.RoutingInfo != null)
                    {
                        suspendedMessage.PartnerIdentifier = outboundMessage.RoutingInfo.PartnerId;
                    }
                }

                this.suspendedTableServiceContext.AddObject(MaargConfiguration.Instance[MaargConfiguration.SuspendedMessageTableName], suspendedMessage);

                //save to suspended table and blob
                DataServiceResponse response = this.suspendedTableServiceContext.SaveChangesWithRetries();
                // The entry in the Q is the message blob name
                using (Stream stream = BizpipeMesssageStreaming.WriteBizpipeMessageToStream(message))
                {
                    CloudBlob blob = this.suspendedContainer.GetBlobReference(suspendedMessage.SuspendedMessageBlobReference);
                    blob.UploadFromStream(stream);
                }

                watch.Stop();
                LoggerFactory.Logger.Debug(location,
                                           "Message {0} saved to suspended queue in {1} ms.",
                                           message.Header.Identifier,
                                           watch.ElapsedMilliseconds);
            }
            catch (Exception exception)
            {
                LoggerFactory.Logger.Error(location, EventId.BizpipeSaveSuspendMessage
                                           , "Exception encountered during suspended message operation: {0}."
                                           , exception.ToString());
                result = false;
            }

            return(result);
        }
        public bool DeleteSuspendedMessage(string tenantIdentifier, string messageIdentifier)
        {
            string location = "FpmDal->DeleteSuspendedMessage";
            bool   result   = true;
            SuspendedMessageEntity suspendedMessage = null;

            try
            {
                Stopwatch watch = Stopwatch.StartNew();

                suspendedMessage =
                    (from e in this.suspendedTableServiceContext.CreateQuery <SuspendedMessageEntity>(MaargConfiguration.Instance[MaargConfiguration.SuspendedMessageTableName])
                     where e.PartitionKey == tenantIdentifier && e.RowKey == messageIdentifier
                     select e).FirstOrDefault();

                if (suspendedMessage != null)
                {
                    //delete from suspended table
                    this.suspendedTableServiceContext.DeleteObject(suspendedMessage);
                    this.suspendedTableServiceContext.SaveChangesWithRetries();

                    //delete from suspended blob
                    CloudBlob blob = this.suspendedContainer.GetBlobReference(suspendedMessage.SuspendedMessageBlobReference);
                    result = blob.DeleteIfExists();
                }

                watch.Stop();
                LoggerFactory.Logger.Info(location,
                                          "Message {0} deleted from suspended queue in {1} ms.",
                                          messageIdentifier,
                                          watch.ElapsedMilliseconds);
            }
            catch (Exception exception)
            {
                result = false;
                LoggerFactory.Logger.Error(location, EventId.BizpipeDeleteSuspendMessage
                                           , "Error deleting the suspended message {0}: {1}."
                                           , messageIdentifier
                                           , exception.ToString());
            }

            return(result);
        }