private void OnCloseCore(bool isAborting, TimeSpan timeout)
 {
     if (!isAborting && (this.buffers.Count > 1))
     {
         lock (base.ThisLock)
         {
             this.VerifyTransaction();
             this.buffers.Add(this.EncodeEndMarker());
         }
         int bodySize = this.CalcSessionGramSize();
         using (MsmqOutputMessage <IOutputSessionChannel> message = new MsmqOutputMessage <IOutputSessionChannel>(this.Factory, bodySize, this.RemoteAddress))
         {
             message.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
             message.Body.EnsureBufferLength(bodySize);
             message.Body.BufferLength = bodySize;
             this.CopySessionGramToBuffer(message.Body.Buffer);
             bool lockHeld = false;
             try
             {
                 Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                 this.msmqQueue.Send(message, MsmqTransactionMode.CurrentOrSingle);
                 MsmqDiagnostics.SessiongramSent(this.Session.Id, message.MessageId, this.buffers.Count);
             }
             catch (MsmqException exception)
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
             }
             finally
             {
                 if (lockHeld)
                 {
                     Msmq.LeaveXPSendLock();
                 }
                 this.ReturnSessionGramBuffers();
             }
         }
     }
     if (this.msmqQueue != null)
     {
         this.msmqQueue.Dispose();
     }
     this.msmqQueue = null;
     if (this.certificateTokenProvider != null)
     {
         if (isAborting)
         {
             this.certificateTokenProvider.Abort();
         }
         else
         {
             this.certificateTokenProvider.Close(timeout);
         }
     }
 }
Esempio n. 2
0
        void OnCloseCore(bool isAborting, TimeSpan timeout)
        {
            // Dump the messages into the queue as a big bag.
            // no MSMQ send if aborting
            // no MSMQ send if the channel has only a preamble (no actual messages sent)
            if (!isAborting && this.buffers.Count > 1)
            {
                lock (ThisLock)
                {
                    VerifyTransaction();

                    buffers.Add(EncodeEndMarker());
                }

                int size = CalcSessionGramSize();

                using (MsmqOutputMessage <IOutputSessionChannel> msmqMessage = new MsmqOutputMessage <IOutputSessionChannel>(this.Factory, size, this.RemoteAddress))
                {
                    msmqMessage.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
                    msmqMessage.Body.EnsureBufferLength(size);
                    msmqMessage.Body.BufferLength = size;
                    CopySessionGramToBuffer(msmqMessage.Body.Buffer);

                    bool lockHeld = false;
                    try
                    {
                        Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                        this.msmqQueue.Send(msmqMessage, MsmqTransactionMode.CurrentOrSingle);
                        MsmqDiagnostics.SessiongramSent(this.Session.Id, msmqMessage.MessageId, this.buffers.Count);
                    }
                    catch (MsmqException ex)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
                    }
                    finally
                    {
                        if (lockHeld)
                        {
                            Msmq.LeaveXPSendLock();
                        }
                        ReturnSessionGramBuffers();
                    }
                }
            }

            if (null != this.msmqQueue)
            {
                this.msmqQueue.Dispose();
            }
            this.msmqQueue = null;

            if (certificateTokenProvider != null)
            {
                if (isAborting)
                {
                    certificateTokenProvider.Abort();
                }
                else
                {
                    certificateTokenProvider.Close(timeout);
                }
            }
        }