public SendAsyncResult(SendAvailabilityPairedNamespaceMessageSender sender, TrackingContext trackingContext, IEnumerable <BrokeredMessage> messages, TimeSpan timeout, AsyncCallback callback, object state) : base(timeout, callback, state)
 {
     this.sender          = sender;
     this.trackingContext = trackingContext;
     this.messages        = messages;
     this.messageBuffer   = new List <BrokeredMessage>();
     try
     {
         foreach (BrokeredMessage message in this.messages)
         {
             message.IsConsumed = false;
             this.messageBuffer.Add(message.Clone());
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRetryPolicyStreamNotClonable(this.trackingContext.Activity, this.trackingContext.TrackingId, "PairnedNamespaceSender", "Send", exception.GetType().FullName, exception.Message));
         foreach (BrokeredMessage brokeredMessage in this.messageBuffer)
         {
             brokeredMessage.Dispose();
         }
         this.messageBuffer.Clear();
     }
     base.Start();
 }
            protected override IEnumerator <IteratorAsyncResult <SendAvailabilityMessagePump.SendAsyncResult> .AsyncStep> GetAsyncSteps()
            {
                using (BrokeredMessage brokeredMessage = SendAvailabilityPairedNamespaceMessageSender.ConvertMessageForPrimary(this.message, this.messageSender.Path))
                {
                    if (brokeredMessage == null || !this.pump.CanPump())
                    {
                    }
                    else
                    {
                        MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWritePairedNamespaceSendingMessage(this.sequenceNumber, this.messageSender.Path));
                        SendAvailabilityMessagePump.SendAsyncResult sendAsyncResult = this;
                        IteratorAsyncResult <SendAvailabilityMessagePump.SendAsyncResult> .BeginCall beginCall = (SendAvailabilityMessagePump.SendAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.messageSender.BeginSend(brokeredMessage, t, c, s);
                        yield return(sendAsyncResult.CallAsync(beginCall, (SendAvailabilityMessagePump.SendAsyncResult thisPtr, IAsyncResult r) => thisPtr.messageSender.EndSend(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                        if (base.LastAsyncStepException != null)
                        {
                            MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWritePairedNamespaceDestinationSendException(this.pump.options.PrimaryMessagingFactory.Address.ToString(), this.messageSender.Path, base.LastAsyncStepException));
                            MessagingException lastAsyncStepException = base.LastAsyncStepException as MessagingException;
                            if (lastAsyncStepException != null)
                            {
                                if (!lastAsyncStepException.IsTransient && !(lastAsyncStepException is QuotaExceededException))
                                {
                                    MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWritePairedNamespaceDeadletterException(this.messageSender.MessagingFactory.Address.ToString(), this.messageSender.Path, lastAsyncStepException));
                                    this.message.DeadLetter(SRClient.BacklogDeadletterReasonNotRetryable, SRClient.BacklogDeadletterDescriptionNotRetryable(this.messageSender.Path, lastAsyncStepException.GetType().Name, lastAsyncStepException.Message));
                                }
                                base.Complete(base.LastAsyncStepException);
                            }
                            else
                            {
                                base.Complete(base.LastAsyncStepException);
                            }
                        }
                        else
                        {
                            SendAvailabilityMessagePump.SendAsyncResult sendAsyncResult1 = this;
                            IteratorAsyncResult <SendAvailabilityMessagePump.SendAsyncResult> .BeginCall beginCall1 = (SendAvailabilityMessagePump.SendAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.message.BeginComplete(t, c, s);
                            yield return(sendAsyncResult1.CallAsync(beginCall1, (SendAvailabilityMessagePump.SendAsyncResult thisPtr, IAsyncResult r) => thisPtr.message.EndComplete(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                            if (base.LastAsyncStepException == null)
                            {
                                goto Label0;
                            }
                            MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWritePairedNamespaceDestinationSendException(this.pump.options.PrimaryMessagingFactory.Address.ToString(), this.messageSender.Path, base.LastAsyncStepException));
                            base.Complete(base.LastAsyncStepException);
                        }
                    }
                }
Label0:
                yield break;
            }
        internal static BrokeredMessage ConvertMessageForPrimary(BrokeredMessage message, string path)
        {
            object          obj;
            BrokeredMessage brokeredMessage = message.Clone();

            if (message.Properties.TryGetValue("x-ms-scheduledenqueuetimeutc", out obj))
            {
                if (!(obj is DateTime))
                {
                    SendAvailabilityPairedNamespaceMessageSender.DeadLetterMessageWithInvalidProperty("x-ms-scheduledenqueuetimeutc", obj, typeof(TimeSpan), message);
                    return(null);
                }
                brokeredMessage.ScheduledEnqueueTimeUtc = (DateTime)obj;
                brokeredMessage.Properties.Remove("x-ms-scheduledenqueuetimeutc");
            }
            if (message.Properties.TryGetValue("x-ms-sessionid", out obj))
            {
                string str = obj as string;
                if (str == null)
                {
                    SendAvailabilityPairedNamespaceMessageSender.DeadLetterMessageWithInvalidProperty("x-ms-sessionid", obj, typeof(string), message);
                    return(null);
                }
                brokeredMessage.SessionId = str;
                brokeredMessage.Properties.Remove("x-ms-sessionid");
            }
            if (message.Properties.TryGetValue("x-ms-timetolive", out obj))
            {
                if (!(obj is TimeSpan))
                {
                    SendAvailabilityPairedNamespaceMessageSender.DeadLetterMessageWithInvalidProperty("x-ms-timetolive", obj, typeof(TimeSpan), message);
                    return(null);
                }
                brokeredMessage.TimeToLive = (TimeSpan)obj;
                brokeredMessage.Properties.Remove("x-ms-timetolive");
            }
            if (message.Properties.TryGetValue("x-ms-path", out obj))
            {
                brokeredMessage.Properties.Remove("x-ms-path");
            }
            return(brokeredMessage);
        }
            protected override IEnumerator <IteratorAsyncResult <SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult> .AsyncStep> GetAsyncSteps()
            {
                Exception exception = this.sender.Options.RetrieveNontransientException(this.sender.Primary.Path);

                if (exception != null)
                {
                    throw Fx.Exception.AsError(new SendAvailabilityMessagingException(exception), null);
                }
                if (this.sender.CanUsePrimary())
                {
                    SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult sendAsyncResult = this;
                    IteratorAsyncResult <SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult> .BeginCall beginCall = (SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.sender.Primary.BeginSend(thisPtr.trackingContext, thisPtr.messages, t, c, s);
                    yield return(sendAsyncResult.CallAsync(beginCall, (SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult thisPtr, IAsyncResult r) => thisPtr.sender.Primary.EndSend(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));

                    if (base.LastAsyncStepException != null)
                    {
                        if (!this.sender.Options.ExceptionInspector.CausesFailover(base.LastAsyncStepException))
                        {
                            goto Label1;
                        }
                        this.sender.Options.NotifyPrimarySendResult(this.sender.Primary.Path, false);
                    }
                    else
                    {
                        this.sender.Options.MarkPathHealthy(this.sender.Path);
                        goto Label0;
                    }
                }
                if (!this.sender.CanUsePrimary())
                {
                    if (this.messageBuffer.Count > 0)
                    {
                        SendAvailabilityPairedNamespaceMessageSender.ConvertMessagesForBacklog(this.messageBuffer, this.sender.Primary.Path);
                        while (base.RemainingTime() > this.SafeBacklogAttempt || base.RemainingTime() > TimeSpan.FromTicks(base.OriginalTimeout.Ticks / (long)2))
                        {
                            List <BrokeredMessage> list             = null;
                            List <BrokeredMessage> brokeredMessages = this.messageBuffer;
                            list = (
                                from msg in brokeredMessages
                                select msg.Clone()).ToList <BrokeredMessage>();
                            try
                            {
                                SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult sendAsyncResult1 = this;
                                IteratorAsyncResult <SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult> .BeginCall beginCall1 = (SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult thisPtr, TimeSpan t, AsyncCallback c, object s) => thisPtr.sender.Backlog.BeginSend(list, t, c, s);
                                yield return(sendAsyncResult1.CallAsync(beginCall1, (SendAvailabilityPairedNamespaceMessageSender.SendAsyncResult thisPtr, IAsyncResult r) => thisPtr.sender.Backlog.EndSend(r), IteratorAsyncResult <TIteratorAsyncResult> .ExceptionPolicy.Continue));
                            }
                            finally
                            {
                                if (list != null)
                                {
                                    list.ForEach((BrokeredMessage msg) => msg.Dispose());
                                    list.Clear();
                                }
                            }
                            if (base.LastAsyncStepException == null)
                            {
                                break;
                            }
                            MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWritePairedNamespaceSendToBacklogFailed(this.sender.MessagingFactory.Address.ToString(), this.sender.Backlog.Path, base.LastAsyncStepException));
                            if (!(base.RemainingTime() > TimeSpan.Zero) || this.sender.Options.BacklogQueueCount <= 1)
                            {
                                break;
                            }
                            this.sender.FindNewMessageSender(this.sender.Backlog.Path);
                        }
                    }
                    List <BrokeredMessage> brokeredMessages1 = this.messageBuffer;
                    brokeredMessages1.ForEach((BrokeredMessage msg) => msg.Dispose());
                    this.messageBuffer.Clear();
                    base.Complete(base.LastAsyncStepException);
                }
                else
                {
                    base.Complete(base.LastAsyncStepException);
                }
Label0:
                yield break;
Label1:
                base.Complete(base.LastAsyncStepException);
                goto Label0;
            }