Exemple #1
0
        internal void EnsureLockQueuesOpen()
        {
            int num = 0;

            while (true)
            {
                try
                {
                    this.lockQueueForReceive.EnsureOpen();
                    break;
                }
                catch (MsmqException exception)
                {
                    if (num >= 3)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
                    }
                    MsmqDiagnostics.ExpectedException(exception);
                }
                this.lockQueueForReceive.Dispose();
                this.lockQueueForMove.Dispose();
                this.lockQueueName       = base.formatName + ";" + GenerateLockQueueName();
                this.lockQueueForReceive = new MsmqQueue(this.lockQueueName, 1, 1);
                this.lockQueueForMove    = new MsmqQueue(this.lockQueueName, 4);
                num++;
            }
            this.lockQueueForMove.EnsureOpen();
        }
Exemple #2
0
        private static void OnPeekCompleted(IAsyncResult result)
        {
            Msmq4PoisonHandler asyncState = result.AsyncState as Msmq4PoisonHandler;

            MsmqQueue.ReceiveResult unknown = MsmqQueue.ReceiveResult.Unknown;
            try
            {
                unknown = asyncState.retryQueueForPeek.EndPeek(result);
            }
            catch (MsmqException exception)
            {
                MsmqDiagnostics.ExpectedException(exception);
            }
            if (MsmqQueue.ReceiveResult.MessageReceived == unknown)
            {
                lock (asyncState)
                {
                    if (!asyncState.disposed)
                    {
                        TimeSpan timeFromNow = (TimeSpan)((MsmqDateTime.ToDateTime(asyncState.retryQueueMessage.LastMoveTime.Value) + asyncState.ReceiveParameters.RetryCycleDelay) - DateTime.UtcNow);
                        if (timeFromNow < TimeSpan.Zero)
                        {
                            asyncState.OnTimer(asyncState);
                        }
                        else
                        {
                            asyncState.timer.Set(timeFromNow);
                        }
                    }
                }
            }
        }
        private void CollectLocks(MsmqQueue lockQueue)
        {
            ReceiveResult result = ReceiveResult.MessageReceived;

            while (result == ReceiveResult.MessageReceived)
            {
                using (MsmqMessageLookupId message = new MsmqMessageLookupId())
                {
                    try
                    {
                        result = lockQueue.TryPeek(message, TimeSpan.FromSeconds(0));
                        if (result == ReceiveResult.MessageReceived)
                        {
                            lockQueue.TryMoveMessage(message.lookupId.Value, this.mainQueueForMove, MsmqTransactionMode.None);
                        }
                    }
                    catch (MsmqException ex)
                    {
                        // we will retry the collection in the next cleanup round
                        MsmqDiagnostics.ExpectedException(ex);
                        result = ReceiveResult.Unknown;
                    }
                }
            }
        }
        internal void EnsureLockQueuesOpen()
        {
            int attempts = 0;

            // handle lock queue name collisions, if we fail three times in a row it is probably not the name
            // collision that is causing the open to fail
            while (true)
            {
                try
                {
                    this.lockQueueForReceive.EnsureOpen();
                    break;
                }
                catch (MsmqException ex)
                {
                    if (attempts >= 3)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex);
                    }
                    MsmqDiagnostics.ExpectedException(ex);
                }

                this.lockQueueForReceive.Dispose();
                this.lockQueueForMove.Dispose();

                this.lockQueueName       = this.formatName + ";" + MsmqSubqueueLockingQueue.GenerateLockQueueName();
                this.lockQueueForReceive = new MsmqQueue(this.lockQueueName, UnsafeNativeMethods.MQ_RECEIVE_ACCESS, UnsafeNativeMethods.MQ_DENY_RECEIVE_SHARE);
                this.lockQueueForMove    = new MsmqQueue(this.lockQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
                attempts++;
            }
            this.lockQueueForMove.EnsureOpen();
        }
Exemple #5
0
            unsafe void StartReceive(bool synchronously)
            {
                bool useCompletionPort;

                try
                {
                    this.handle = this.msmqQueue.GetHandleForAsync(out useCompletionPort);
                }
                catch (MsmqException ex)
                {
                    OnCompletion(ex.ErrorCode, synchronously);
                    return;
                }
                if (null != nativeOverlapped)
                {
                    Fx.Assert("---- in StartReceive");
                }

                IntPtr nativePropertiesPointer = this.message.Pin();

                nativeOverlapped = new Overlapped(0, 0, IntPtr.Zero, this).UnsafePack(onPortedCompletion, this.message.GetBuffersForAsync());

                int error;

                try
                {
                    if (useCompletionPort)
                    {
                        error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(),
                                                           this.action, nativeOverlapped, null);
                    }
                    else
                    {
                        if (onNonPortedCompletion == null)
                        {
                            onNonPortedCompletion = new UnsafeNativeMethods.MQReceiveCallback(OnNonPortedCompletion);
                        }
                        error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(),
                                                           this.action, nativeOverlapped, onNonPortedCompletion);
                    }
                }
                catch (ObjectDisposedException ex)
                {
                    // if Close ----s with the async Receive, it is possible that SafeHandle will throw ObjectDisposedException
                    // the behavior should be same as if operation was just cancelled (the channel will return no message)
                    MsmqDiagnostics.ExpectedException(ex);
                    error = UnsafeNativeMethods.MQ_ERROR_OPERATION_CANCELLED;
                }
                if (error != 0)
                {
                    if (error != UnsafeNativeMethods.MQ_INFORMATION_OPERATION_PENDING)
                    {
                        Overlapped.Free(nativeOverlapped);
                        nativeOverlapped = null;
                        GC.SuppressFinalize(this);
                        OnCompletion(error, synchronously);
                    }
                }
            }
 private void OnReceiveContextFaulted(object sender, EventArgs e)
 {
     try
     {
         MsmqReceiveContext receiveContext = (MsmqReceiveContext)sender;
         this.UnlockMessage(receiveContext, TimeSpan.Zero);
     }
     catch (MsmqException exception)
     {
         MsmqDiagnostics.ExpectedException(exception);
     }
 }
 private bool CheckServiceExists(string serviceFile)
 {
     try
     {
         return(ServiceHostingEnvironment.IsConfigurationBasedService(serviceFile) || HostingEnvironmentWrapper.ServiceFileExists(serviceFile));
     }
     catch (ArgumentException exception)
     {
         MsmqDiagnostics.ExpectedException(exception);
         return(false);
     }
 }
 void OnReceiveContextFaulted(object sender, EventArgs e)
 {
     try
     {
         MsmqReceiveContext receiveContext = (MsmqReceiveContext)sender;
         UnlockMessage(receiveContext, TimeSpan.Zero);
     }
     catch (MsmqException ex)
     {
         // ReceiveContext is already faulted and best effort was made to cleanup the lock queue.
         MsmqDiagnostics.ExpectedException(ex);
     }
 }
 bool CheckServiceExists(string serviceFile)
 {
     try
     {
         return((ServiceHostingEnvironment.IsConfigurationBasedService(serviceFile) ||
                 HostingEnvironmentWrapper.ServiceFileExists(serviceFile)) &&
                AspNetEnvironment.Current.IsWithinApp(VirtualPathUtility.ToAbsolute(serviceFile)));
     }
     catch (ArgumentException ex)
     {
         MsmqDiagnostics.ExpectedException(ex);
         return(false);
     }
 }
Exemple #10
0
        public override MsmqQueue.ReceiveResult TryReceive(NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode)
        {
            MsmqQueue.MoveReceiveResult result3;
            TimeoutHelper helper   = new TimeoutHelper(timeout);
            bool          flag     = false;
            long          lookupId = 0L;

            while (!flag)
            {
                using (MsmqMessageLookupId id = new MsmqMessageLookupId())
                {
                    MsmqQueue.ReceiveResult result = base.TryPeek(id, helper.RemainingTime());
                    if (result != MsmqQueue.ReceiveResult.MessageReceived)
                    {
                        return(result);
                    }
                    lookupId = id.lookupId.Value;
                }
                try
                {
                    if (base.TryMoveMessage(lookupId, this.lockQueueForMove, MsmqTransactionMode.None) == MsmqQueue.MoveReceiveResult.Succeeded)
                    {
                        flag = true;
                    }
                    continue;
                }
                catch (MsmqException exception)
                {
                    MsmqDiagnostics.ExpectedException(exception);
                    continue;
                }
            }
            try
            {
                result3 = this.lockQueueForReceive.TryReceiveByLookupId(lookupId, message, MsmqTransactionMode.None, 0x40000010);
            }
            catch (MsmqException exception2)
            {
                this.UnlockMessage(lookupId, TimeSpan.Zero);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception2);
            }
            if (result3 == MsmqQueue.MoveReceiveResult.Succeeded)
            {
                return(MsmqQueue.ReceiveResult.MessageReceived);
            }
            this.UnlockMessage(lookupId, TimeSpan.Zero);
            return(MsmqQueue.ReceiveResult.OperationCancelled);
        }
 private void StartService(object state)
 {
     try
     {
         string virtualPath = (string)state;
         ServiceHostingEnvironment.EnsureServiceAvailable(virtualPath);
     }
     catch (ServiceActivationException exception)
     {
         MsmqDiagnostics.ExpectedException(exception);
     }
     catch (EndpointNotFoundException exception2)
     {
         MsmqDiagnostics.ExpectedException(exception2);
     }
 }
Exemple #12
0
        public MoveReceiveResult TryMoveMessage(long lookupId, MsmqQueue destinationQueue, MsmqTransactionMode transactionMode)
        {
            MsmqQueueHandle sourceQueueHandle      = GetHandle();
            MsmqQueueHandle destinationQueueHandle = destinationQueue.GetHandle();
            int             error;

            try
            {
                if (RequiresDtcTransaction(transactionMode))
                {
                    error = TryMoveMessageDtcTransacted(lookupId, sourceQueueHandle, destinationQueueHandle, transactionMode);
                }
                else
                {
                    error = UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, destinationQueueHandle,
                                                              lookupId, (IntPtr)GetTransactionConstant(transactionMode));
                }
            }
            catch (ObjectDisposedException ex)
            {
                MsmqDiagnostics.ExpectedException(ex);
                return(MoveReceiveResult.Succeeded);
            }
            if (error != 0)
            {
                if (error == UnsafeNativeMethods.MQ_ERROR_MESSAGE_NOT_FOUND)
                {
                    return(MoveReceiveResult.MessageNotFound);
                }
                else if (error == UnsafeNativeMethods.MQ_ERROR_MESSAGE_LOCKED_UNDER_TRANSACTION)
                {
                    return(MoveReceiveResult.MessageLockedUnderTransaction);
                }

                else if (IsErrorDueToStaleHandle(error))
                {
                    HandleIsStale(sourceQueueHandle);
                    destinationQueue.HandleIsStale(destinationQueueHandle);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqSendError,
                                                                                                         MsmqError.GetErrorString(error)), error));
            }

            return(MoveReceiveResult.Succeeded);
        }
            private unsafe void StartReceive(bool synchronously)
            {
                bool flag;
                int  num;

                try
                {
                    this.handle = this.msmqQueue.GetHandleForAsync(out flag);
                }
                catch (MsmqException exception)
                {
                    this.OnCompletion(exception.ErrorCode, synchronously);
                    return;
                }
                NativeOverlapped *nativeOverlapped        = this.nativeOverlapped;
                IntPtr            nativePropertiesPointer = this.message.Pin();

                this.nativeOverlapped = new Overlapped(0, 0, IntPtr.Zero, this).UnsafePack(onPortedCompletion, this.message.GetBuffersForAsync());
                try
                {
                    if (flag)
                    {
                        num = this.msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), this.action, this.nativeOverlapped, null);
                    }
                    else
                    {
                        if (onNonPortedCompletion == null)
                        {
                            onNonPortedCompletion = new UnsafeNativeMethods.MQReceiveCallback(MsmqQueue.TryReceiveAsyncResult.OnNonPortedCompletion);
                        }
                        num = this.msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), this.action, this.nativeOverlapped, onNonPortedCompletion);
                    }
                }
                catch (ObjectDisposedException exception2)
                {
                    MsmqDiagnostics.ExpectedException(exception2);
                    num = -1072824312;
                }
                if ((num != 0) && (num != 0x400e0006))
                {
                    Overlapped.Free(this.nativeOverlapped);
                    this.nativeOverlapped = null;
                    GC.SuppressFinalize(this);
                    this.OnCompletion(num, synchronously);
                }
            }
 void OnTimer(object state)
 {
     lock (this)
     {
         if (!this.disposed)
         {
             try
             {
                 this.retryQueueForPeek.TryMoveMessage(this.retryQueueMessage.LookupId.Value, this.mainQueueForMove, MsmqTransactionMode.Single);
             }
             catch (MsmqException ex)
             {
                 MsmqDiagnostics.ExpectedException(ex);
             }
             this.retryQueueForPeek.BeginPeek(this.retryQueueMessage, TimeSpan.MaxValue, onPeekCompleted, this);
         }
     }
 }
Exemple #15
0
 internal static void TryAbortTransactionCurrent()
 {
     if (null != Transaction.Current)
     {
         try
         {
             Transaction.Current.Rollback();
         }
         catch (TransactionAbortedException ex)
         {
             MsmqDiagnostics.ExpectedException(ex);
         }
         catch (ObjectDisposedException ex)
         {
             MsmqDiagnostics.ExpectedException(ex);
         }
     }
 }
 private void RollbackTransaction(Exception exception)
 {
     try
     {
         if (this.associatedTx.TransactionInformation.Status == TransactionStatus.Active)
         {
             this.associatedTx.Rollback(exception);
         }
     }
     catch (TransactionAbortedException exception2)
     {
         MsmqDiagnostics.ExpectedException(exception2);
     }
     catch (ObjectDisposedException exception3)
     {
         MsmqDiagnostics.ExpectedException(exception3);
     }
 }
Exemple #17
0
        public MoveReceiveResult TryReceiveByLookupId(long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            MsmqQueueHandle handle = GetHandle();
            int             error  = 0;

            while (true)
            {
                try
                {
                    error = ReceiveByLookupIdCore(handle, lookupId, message, transactionMode, action);
                }
                catch (ObjectDisposedException ex)
                {
                    // ---- with Close
                    MsmqDiagnostics.ExpectedException(ex);
                    return(MoveReceiveResult.Succeeded);
                }

                if (0 == error)
                {
                    return(MoveReceiveResult.Succeeded);
                }

                if (IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    message.GrowBuffers();
                    continue;
                }
                else if (UnsafeNativeMethods.MQ_ERROR_MESSAGE_NOT_FOUND == error)
                {
                    return(MoveReceiveResult.MessageNotFound);
                }
                else if (UnsafeNativeMethods.MQ_ERROR_MESSAGE_LOCKED_UNDER_TRANSACTION == error)
                {
                    return(MoveReceiveResult.MessageLockedUnderTransaction);
                }
                else if (IsErrorDueToStaleHandle(error))
                {
                    HandleIsStale(handle);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqReceiveError, MsmqError.GetErrorString(error)), error));
            }
        }
 protected override Uri PostVerify(Uri uri)
 {
     if (0 == String.Compare(uri.Host, "localhost", StringComparison.OrdinalIgnoreCase))
     {
         return(uri);
     }
     try
     {
         if (0 == String.Compare(DnsCache.MachineName, DnsCache.Resolve(uri).HostName, StringComparison.OrdinalIgnoreCase))
         {
             return(new UriBuilder(Scheme, "localhost", -1, uri.PathAndQuery).Uri);
         }
     }
     catch (EndpointNotFoundException ex)
     {
         MsmqDiagnostics.ExpectedException(ex);
     }
     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MsmqDLQNotLocal), "uri"));
 }
        void CleanupExpiredLocks(object state)
        {
            lock (this.internalStateLock)
            {
                if (this.disposed)
                {
                    return;
                }

                if ((this.messageExpiryMap.Count < 1))
                {
                    this.messageExpiryTimer.Set(this.messageTimeoutInterval);
                    return;
                }

                List <MsmqReceiveContext> expiredLockList = new List <MsmqReceiveContext>();
                try
                {
                    foreach (KeyValuePair <long, MsmqReceiveContext> msgEntry in this.messageExpiryMap)
                    {
                        if (DateTime.UtcNow > msgEntry.Value.ExpiryTime)
                        {
                            expiredLockList.Add(msgEntry.Value);
                        }
                    }
                    try
                    {
                        foreach (MsmqReceiveContext entry in expiredLockList)
                        {
                            entry.MarkContextExpired();
                        }
                    }
                    catch (MsmqException ex)
                    {
                        MsmqDiagnostics.ExpectedException(ex);
                    }
                }
                finally
                {
                    this.messageExpiryTimer.Set(this.messageTimeoutInterval);
                }
            }
        }
 private void CleanupExpiredLocks(object state)
 {
     lock (this.internalStateLock)
     {
         if (!this.disposed)
         {
             if (this.messageExpiryMap.Count < 1)
             {
                 this.messageExpiryTimer.Set(this.messageTimeoutInterval);
             }
             else
             {
                 List <MsmqReceiveContext> list = new List <MsmqReceiveContext>();
                 try
                 {
                     foreach (KeyValuePair <long, MsmqReceiveContext> pair in this.messageExpiryMap)
                     {
                         if (DateTime.UtcNow > pair.Value.ExpiryTime)
                         {
                             list.Add(pair.Value);
                         }
                     }
                     try
                     {
                         foreach (MsmqReceiveContext context in list)
                         {
                             context.MarkContextExpired();
                         }
                     }
                     catch (MsmqException exception)
                     {
                         MsmqDiagnostics.ExpectedException(exception);
                     }
                 }
                 finally
                 {
                     this.messageExpiryTimer.Set(this.messageTimeoutInterval);
                 }
             }
         }
     }
 }
 void StartService(object state)
 {
     try
     {
         string processedVirtualPath = (string)state;
         ServiceHostingEnvironment.EnsureServiceAvailable(processedVirtualPath);
     }
     catch (ServiceActivationException e)
     {
         // Non-fatal exceptions from the user code are wrapped in ServiceActivationException
         // The best we can do is to trace them
         MsmqDiagnostics.ExpectedException(e);
     }
     catch (EndpointNotFoundException e)
     {
         // This means that the server disappeared between the time we
         // saw the service, and the time we tried to start it.
         // That's okay.
         MsmqDiagnostics.ExpectedException(e);
     }
 }
        public MoveReceiveResult TryMoveMessage(long lookupId, MsmqQueue destinationQueue, MsmqTransactionMode transactionMode)
        {
            int             num;
            MsmqQueueHandle sourceQueueHandle = this.GetHandle();
            MsmqQueueHandle handle            = destinationQueue.GetHandle();

            try
            {
                if (this.RequiresDtcTransaction(transactionMode))
                {
                    num = this.TryMoveMessageDtcTransacted(lookupId, sourceQueueHandle, handle, transactionMode);
                }
                else
                {
                    num = UnsafeNativeMethods.MQMoveMessage(sourceQueueHandle, handle, lookupId, (IntPtr)this.GetTransactionConstant(transactionMode));
                }
            }
            catch (ObjectDisposedException exception)
            {
                MsmqDiagnostics.ExpectedException(exception);
                return(MoveReceiveResult.Succeeded);
            }
            switch (num)
            {
            case 0:
                return(MoveReceiveResult.Succeeded);

            case -1072824184:
                return(MoveReceiveResult.MessageNotFound);

            case -1072824164:
                return(MoveReceiveResult.MessageLockedUnderTransaction);
            }
            if (IsErrorDueToStaleHandle(num))
            {
                this.HandleIsStale(sourceQueueHandle);
                destinationQueue.HandleIsStale(handle);
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqSendError", new object[] { MsmqError.GetErrorString(num) }), num));
        }
        public void MarkMessageRejected(long lookupId)
        {
            MsmqQueueHandle handle = this.GetHandle();
            int             error  = 0;

            try
            {
                error = UnsafeNativeMethods.MQMarkMessageRejected(handle, lookupId);
            }
            catch (ObjectDisposedException exception)
            {
                MsmqDiagnostics.ExpectedException(exception);
            }
            if (error != 0)
            {
                if (IsErrorDueToStaleHandle(error))
                {
                    this.HandleIsStale(handle);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqSendError", new object[] { MsmqError.GetErrorString(error) }), error));
            }
        }
        public MoveReceiveResult TryReceiveByLookupId(long lookupId, NativeMsmqMessage message, MsmqTransactionMode transactionMode, int action)
        {
            MsmqQueueHandle handle = this.GetHandle();
            int             error  = 0;

            while (true)
            {
                try
                {
                    error = this.ReceiveByLookupIdCore(handle, lookupId, message, transactionMode, action);
                }
                catch (ObjectDisposedException exception)
                {
                    MsmqDiagnostics.ExpectedException(exception);
                    return(MoveReceiveResult.Succeeded);
                }
                if (error == 0)
                {
                    return(MoveReceiveResult.Succeeded);
                }
                if (!IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    if (-1072824184 == error)
                    {
                        return(MoveReceiveResult.MessageNotFound);
                    }
                    if (-1072824164 == error)
                    {
                        return(MoveReceiveResult.MessageLockedUnderTransaction);
                    }
                    if (IsErrorDueToStaleHandle(error))
                    {
                        this.HandleIsStale(handle);
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqReceiveError", new object[] { MsmqError.GetErrorString(error) }), error));
                }
                message.GrowBuffers();
            }
        }
        static void OnPeekCompleted(IAsyncResult result)
        {
            Msmq4PoisonHandler handler = result.AsyncState as Msmq4PoisonHandler;

            MsmqQueue.ReceiveResult receiveResult = MsmqQueue.ReceiveResult.Unknown;
            try
            {
                receiveResult = handler.retryQueueForPeek.EndPeek(result);
            }
            catch (MsmqException ex)
            {
                MsmqDiagnostics.ExpectedException(ex);
            }

            if (MsmqQueue.ReceiveResult.MessageReceived == receiveResult)
            {
                lock (handler)
                {
                    if (!handler.disposed)
                    {
                        // Check the time - move it, and begin peeking again
                        // if necessary, or wait for the timeout.

                        DateTime lastMoveTime = MsmqDateTime.ToDateTime(handler.retryQueueMessage.LastMoveTime.Value);

                        TimeSpan waitTime = lastMoveTime + handler.ReceiveParameters.RetryCycleDelay - DateTime.UtcNow;
                        if (waitTime < TimeSpan.Zero)
                        {
                            handler.OnTimer(handler);
                        }
                        else
                        {
                            handler.timer.Set(waitTime);
                        }
                    }
                }
            }
        }
Exemple #26
0
 private void CollectLocks(MsmqQueue lockQueue)
 {
     MsmqQueue.ReceiveResult messageReceived = MsmqQueue.ReceiveResult.MessageReceived;
     while (messageReceived == MsmqQueue.ReceiveResult.MessageReceived)
     {
         using (MsmqMessageLookupId id = new MsmqMessageLookupId())
         {
             try
             {
                 messageReceived = lockQueue.TryPeek(id, TimeSpan.FromSeconds(0.0));
                 if (messageReceived == MsmqQueue.ReceiveResult.MessageReceived)
                 {
                     lockQueue.TryMoveMessage(id.lookupId.Value, this.mainQueueForMove, MsmqTransactionMode.None);
                 }
             }
             catch (MsmqException exception)
             {
                 MsmqDiagnostics.ExpectedException(exception);
                 messageReceived = MsmqQueue.ReceiveResult.Unknown;
             }
             continue;
         }
     }
 }
        public override ReceiveResult TryReceive(NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode)
        {
            // we ignore transaction mode for receive context receives
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            bool receivedMessage = false;
            long lookupId        = 0;

            // peek for new message, move it to the lock queue and then receive the full message
            // if move fails because another thread moved it ahead of us then peek again
            while (!receivedMessage)
            {
                ReceiveResult     result;
                MoveReceiveResult moveResult;

                using (MsmqMessageLookupId emptyMessage = new MsmqMessageLookupId())
                {
                    result = this.TryPeek(emptyMessage, timeoutHelper.RemainingTime());
                    if (result != ReceiveResult.MessageReceived)
                    {
                        return(result);
                    }
                    lookupId = emptyMessage.lookupId.Value;
                }

                try
                {
                    moveResult = this.TryMoveMessage(lookupId, this.lockQueueForMove, MsmqTransactionMode.None);
                    if (moveResult == MoveReceiveResult.Succeeded)
                    {
                        receivedMessage = true;
                    }
                }
                catch (MsmqException ex)
                {
                    MsmqDiagnostics.ExpectedException(ex);
                }
            }

            MoveReceiveResult lookupIdReceiveResult;

            try
            {
                lookupIdReceiveResult = this.lockQueueForReceive.TryReceiveByLookupId(lookupId, message, MsmqTransactionMode.None, UnsafeNativeMethods.MQ_LOOKUP_PEEK_CURRENT);
            }
            catch (MsmqException ex)
            {
                this.UnlockMessage(lookupId, TimeSpan.Zero);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex);
            }

            if (lookupIdReceiveResult == MoveReceiveResult.Succeeded)
            {
                return(ReceiveResult.MessageReceived);
            }
            else
            {
                this.UnlockMessage(lookupId, TimeSpan.Zero);
                return(ReceiveResult.OperationCancelled);
            }
        }
        // The demand is not added now (in 4.5), to avoid a breaking change. To be considered in the next version.

        /*
         * // We demand full trust because we call code from a non-APTCA assembly.
         * // MSMQ is not enabled in partial trust, so this demand should not break customers.
         * [PermissionSet(SecurityAction.Demand, Unrestricted = true)]
         */
        public void DeleteMessage(long lookupId, TimeSpan timeout)
        {
            TransactionLookupEntry entry;

            if (Transaction.Current != null && Transaction.Current.TransactionInformation.Status != System.Transactions.TransactionStatus.Active)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqAmbientTransactionInactive)));
            }

            lock (this.internalStateLock)
            {
                if (!this.lockMap.TryGetValue(lookupId, out entry))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MessageNotInLockedState, lookupId)));
                }

                // a failed relock is the same as not having a lock
                if (entry.MsmqInternalTransaction == null)
                {
                    this.lockMap.Remove(entry.LookupId);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MessageNotInLockedState, lookupId)));
                }
            }

            if (Transaction.Current == null)
            {
                entry.MsmqInternalTransaction.Commit(
                    0,                   // fRetaining
                    0,                   // grfTC
                    0                    // grfRM
                    );

                lock (this.internalStateLock)
                {
                    this.lockMap.Remove(lookupId);
                }
            }
            else
            {
                // we don't want any thread receiving the message we are trying to re-receive in a new transaction
                lock (this.receiveLock)
                {
                    MsmqQueueHandle handle = GetHandle();

                    // abort internal transaction and re-receive in the ambient transaction
                    BOID boid = new BOID();
                    entry.MsmqInternalTransaction.Abort(
                        ref boid, // pboidReason
                        0,        // fRetaining
                        0         // fAsync
                        );
                    // null indicates that the associated internal tx was aborted and the message is now
                    // unlocked as far as the native queue manager is concerned
                    entry.MsmqInternalTransaction = null;

                    using (MsmqEmptyMessage emptyMessage = new MsmqEmptyMessage())
                    {
                        int error = 0;
                        try
                        {
                            error = base.ReceiveByLookupIdCoreDtcTransacted(handle, lookupId, emptyMessage,
                                                                            MsmqTransactionMode.CurrentOrThrow, UnsafeNativeMethods.MQ_LOOKUP_RECEIVE_CURRENT);
                        }
                        catch (ObjectDisposedException ex)
                        {
                            // ---- with Close
                            MsmqDiagnostics.ExpectedException(ex);
                        }

                        if (error != 0)
                        {
                            if (IsErrorDueToStaleHandle(error))
                            {
                                HandleIsStale(handle);
                            }

                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqCannotReacquireLock), error));
                        }
                    }
                }

                List <long> transMsgs;
                lock (this.internalStateLock)
                {
                    if (!this.dtcTransMap.TryGetValue(Transaction.Current.TransactionInformation.DistributedIdentifier, out transMsgs))
                    {
                        transMsgs = new List <long>();
                        this.dtcTransMap.Add(Transaction.Current.TransactionInformation.DistributedIdentifier, transMsgs);
                        // only need to attach the tx complete handler once per transaction
                        Transaction.Current.TransactionCompleted += this.transactionCompletedHandler;
                    }
                    transMsgs.Add(lookupId);
                }
            }
        }
Exemple #29
0
        public void DeleteMessage(long lookupId, TimeSpan timeout)
        {
            TransactionLookupEntry entry;

            if ((Transaction.Current != null) && (Transaction.Current.TransactionInformation.Status != System.Transactions.TransactionStatus.Active))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqAmbientTransactionInactive")));
            }
            lock (this.internalStateLock)
            {
                if (!this.lockMap.TryGetValue(lookupId, out entry))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MessageNotInLockedState", new object[] { lookupId })));
                }
                if (entry.MsmqInternalTransaction == null)
                {
                    this.lockMap.Remove(entry.LookupId);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MessageNotInLockedState", new object[] { lookupId })));
                }
            }
            if (Transaction.Current == null)
            {
                entry.MsmqInternalTransaction.Commit(0, 0, 0);
                lock (this.internalStateLock)
                {
                    this.lockMap.Remove(lookupId);
                    return;
                }
            }
            lock (this.receiveLock)
            {
                MsmqQueueHandle handle      = base.GetHandle();
                BOID            pboidReason = new BOID();
                entry.MsmqInternalTransaction.Abort(ref pboidReason, 0, 0);
                entry.MsmqInternalTransaction = null;
                using (MsmqEmptyMessage message = new MsmqEmptyMessage())
                {
                    int error = 0;
                    try
                    {
                        error = base.ReceiveByLookupIdCoreDtcTransacted(handle, lookupId, message, MsmqTransactionMode.CurrentOrThrow, 0x40000020);
                    }
                    catch (ObjectDisposedException exception)
                    {
                        MsmqDiagnostics.ExpectedException(exception);
                    }
                    if (error != 0)
                    {
                        if (MsmqQueue.IsErrorDueToStaleHandle(error))
                        {
                            base.HandleIsStale(handle);
                        }
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqCannotReacquireLock"), error));
                    }
                }
            }
            lock (this.internalStateLock)
            {
                List <long> list;
                if (!this.dtcTransMap.TryGetValue(Transaction.Current.TransactionInformation.DistributedIdentifier, out list))
                {
                    list = new List <long>();
                    this.dtcTransMap.Add(Transaction.Current.TransactionInformation.DistributedIdentifier, list);
                    Transaction.Current.TransactionCompleted += this.transactionCompletedHandler;
                }
                list.Add(lookupId);
            }
        }
Exemple #30
0
        internal static void VerifyReceiver(MsmqReceiveParameters receiveParameters, Uri listenUri)
        {
            MsmqException exception;
            bool          flag2;

            if (!receiveParameters.Durable && receiveParameters.ExactlyOnce)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqNoAssurancesForVolatile")));
            }
            if (receiveParameters.ReceiveContextSettings.Enabled && !receiveParameters.ExactlyOnce)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqExactlyOnceNeededForReceiveContext")));
            }
            VerifySecurity(receiveParameters.TransportSecurity, null);
            string formatName = receiveParameters.AddressTranslator.UriToFormatName(listenUri);

            if (receiveParameters.ReceiveContextSettings.Enabled && formatName.Contains(";"))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqReceiveContextSubqueuesNotSupported")));
            }
            if (!MsmqQueue.IsReadable(formatName, out exception))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqQueueNotReadable"), exception));
            }
            bool flag = false;

            flag = MsmqQueue.TryGetIsTransactional(formatName, out flag2);
            try
            {
                if (!flag && (receiveParameters is MsmqTransportReceiveParameters))
                {
                    flag = MsmqQueue.TryGetIsTransactional(MsmqUri.ActiveDirectoryAddressTranslator.UriToFormatName(listenUri), out flag2);
                }
            }
            catch (MsmqException exception2)
            {
                MsmqDiagnostics.ExpectedException(exception2);
            }
            if (flag)
            {
                if (!receiveParameters.ExactlyOnce && flag2)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqNonTransactionalQueueNeeded")));
                }
                if (receiveParameters.ExactlyOnce && !flag2)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqTransactionalQueueNeeded")));
                }
            }
            if (receiveParameters.ExactlyOnce)
            {
                if (Msmq.IsAdvancedPoisonHandlingSupported)
                {
                    if (!formatName.Contains(";"))
                    {
                        if (!MsmqQueue.IsMoveable(formatName + ";retry"))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqDirectFormatNameRequiredForPoison")));
                        }
                    }
                    else if (ReceiveErrorHandling.Move == receiveParameters.ReceiveErrorHandling)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqNoMoveForSubqueues")));
                    }
                }
                else if ((ReceiveErrorHandling.Reject == receiveParameters.ReceiveErrorHandling) || (ReceiveErrorHandling.Move == receiveParameters.ReceiveErrorHandling))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqAdvancedPoisonHandlingRequired")));
                }
            }
        }