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();
        }
 void CloseQueue()
 {
     this.outputMessages.Dispose();
     if (null != this.msmqQueue)
         this.msmqQueue.Dispose();
     this.msmqQueue = null;
 }
示例#3
0
        private void InternalFinalDisposition(MsmqQueue disposeFromQueue, MsmqMessageProperty messageProperty)
        {
            switch (this.ReceiveParameters.ReceiveErrorHandling)
            {
            case ReceiveErrorHandling.Fault:
                MsmqReceiveHelper.TryAbortTransactionCurrent();
                if (this.receiver.ChannelListener != null)
                {
                    this.receiver.ChannelListener.FaultListener();
                }
                if (this.receiver.Channel == null)
                {
                    break;
                }
                this.receiver.Channel.FaultChannel();
                return;

            case ReceiveErrorHandling.Drop:
                this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, false);
                return;

            case ReceiveErrorHandling.Reject:
                this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, true);
                MsmqDiagnostics.PoisonMessageRejected(messageProperty.MessageId, this.receiver.InstanceId);
                return;

            case ReceiveErrorHandling.Move:
                MsmqReceiveHelper.MoveReceivedMessage(disposeFromQueue, this.poisonQueue, messageProperty.LookupId);
                MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, true, this.receiver.InstanceId);
                break;

            default:
                return;
            }
        }
        public void Open()
        {
            if (this.ReceiveParameters.ReceiveContextSettings.Enabled)
            {
                Fx.Assert(this.receiver.Queue is MsmqSubqueueLockingQueue, "Queue must be MsmqSubqueueLockingQueue");
                this.lockQueueForReceive = ((MsmqSubqueueLockingQueue)this.receiver.Queue).LockQueueForReceive;
            }

            this.mainQueue = this.receiver.Queue;
            this.mainQueueForMove = new MsmqQueue(this.mainQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            // Open up the poison queue (for handling poison messages).
            this.poisonQueue = new MsmqQueue(this.poisonQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            this.retryQueueForMove = new MsmqQueue(this.retryQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            this.retryQueueForPeek = new MsmqQueue(this.retryQueueName, UnsafeNativeMethods.MQ_RECEIVE_ACCESS);
            this.retryQueueMessage = new MsmqRetryQueueMessage();

            if (Thread.CurrentThread.IsThreadPoolThread)
            {
                StartPeek(this);
            }
            else
            {
                ActionItem.Schedule(Msmq4PoisonHandler.onStartPeek, this);
            }
        }
示例#5
0
 public WaitForMessageAsyncResult(MsmqQueue msmqQueue, TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.msmqMessage = new MsmqEmptyMessage();
     this.msmqQueue   = msmqQueue;
     this.msmqQueue.BeginPeek(this.msmqMessage, timeout, onCompleteStatic, this);
 }
示例#6
0
        public override MsmqQueue.ReceiveResult TryReceive(NativeMsmqMessage message, TimeSpan timeout, MsmqTransactionMode transactionMode)
        {
            TimeoutHelper   helper = new TimeoutHelper(timeout);
            MsmqQueueHandle handle = base.GetHandle();

            while (true)
            {
                int error = this.PeekLockCore(handle, (MsmqInputMessage)message, helper.RemainingTime());
                if (error == 0)
                {
                    return(MsmqQueue.ReceiveResult.MessageReceived);
                }
                if (!MsmqQueue.IsReceiveErrorDueToInsufficientBuffer(error))
                {
                    if (error == -1072824293)
                    {
                        return(MsmqQueue.ReceiveResult.Timeout);
                    }
                    if (error == -1072824312)
                    {
                        return(MsmqQueue.ReceiveResult.OperationCancelled);
                    }
                    if (error == -1072824313)
                    {
                        return(MsmqQueue.ReceiveResult.OperationCancelled);
                    }
                    if (MsmqQueue.IsErrorDueToStaleHandle(error))
                    {
                        base.HandleIsStale(handle);
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqReceiveError", new object[] { MsmqError.GetErrorString(error) }), error));
                }
                message.GrowBuffers();
            }
        }
        public MsmqSubqueueLockingQueue(string formatName, string hostname, int accessMode)
            : base(formatName, accessMode)
        {
            // The hostname will be empty for MsmqIntegrationBinding 
            if (string.Compare(hostname, string.Empty, StringComparison.OrdinalIgnoreCase) == 0)
            {
                this.validHostName = MsmqSubqueueLockingQueue.TryGetHostName(formatName, out hostname);
            }
            else
            {
                this.validHostName = true;
            }

            this.disposed = false;
            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);
            this.mainQueueForMove = new MsmqQueue(this.formatName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            this.lockCollectionTimer = new IOThreadTimer(new Action<object>(OnCollectionTimer), null, false);

            if (string.Compare(hostname, "localhost", StringComparison.OrdinalIgnoreCase) == 0)
            {
                this.hostname = null;
            }
            else
            {
                this.hostname = hostname;
            }
        }
        private void InternalFinalDisposition(MsmqQueue disposeFromQueue, MsmqMessageProperty messageProperty)
        {
            switch (this.ReceiveParameters.ReceiveErrorHandling)
            {
            case ReceiveErrorHandling.Drop:
                this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, false);
                break;

            case ReceiveErrorHandling.Fault:
                MsmqReceiveHelper.TryAbortTransactionCurrent();
                if (null != this.receiver.ChannelListener)
                {
                    this.receiver.ChannelListener.FaultListener();
                }
                if (null != this.receiver.Channel)
                {
                    this.receiver.Channel.FaultChannel();
                }
                break;

            case ReceiveErrorHandling.Reject:
                this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, true);
                MsmqDiagnostics.PoisonMessageRejected(messageProperty.MessageId, this.receiver.InstanceId);
                break;

            case ReceiveErrorHandling.Move:
                MsmqReceiveHelper.MoveReceivedMessage(disposeFromQueue, this.poisonQueue, messageProperty.LookupId);
                MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, true, this.receiver.InstanceId);
                break;

            default:
                Fx.Assert("System.ServiceModel.Channels.Msmq4PoisonHandler.FinalDisposition(): (unexpected ReceiveErrorHandling)");
                break;
            }
        }
        public void Open()
        {
            if (this.ReceiveParameters.ReceiveContextSettings.Enabled)
            {
                Fx.Assert(this.receiver.Queue is MsmqSubqueueLockingQueue, "Queue must be MsmqSubqueueLockingQueue");
                this.lockQueueForReceive = ((MsmqSubqueueLockingQueue)this.receiver.Queue).LockQueueForReceive;
            }

            this.mainQueue        = this.receiver.Queue;
            this.mainQueueForMove = new MsmqQueue(this.mainQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            // Open up the poison queue (for handling poison messages).
            this.poisonQueue       = new MsmqQueue(this.poisonQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            this.retryQueueForMove = new MsmqQueue(this.retryQueueName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            this.retryQueueForPeek = new MsmqQueue(this.retryQueueName, UnsafeNativeMethods.MQ_RECEIVE_ACCESS);
            this.retryQueueMessage = new MsmqRetryQueueMessage();

            if (Thread.CurrentThread.IsThreadPoolThread)
            {
                StartPeek(this);
            }
            else
            {
                ActionItem.Schedule(Msmq4PoisonHandler.onStartPeek, this);
            }
        }
 internal void DropOrRejectReceivedMessage(MsmqQueue queue, MsmqMessageProperty messageProperty, bool reject)
 {
     if (this.Transactional)
     {
         TryAbortTransactionCurrent();
         IPostRollbackErrorStrategy strategy = new SimplePostRollbackErrorStrategy(messageProperty.LookupId);
         MsmqQueue.MoveReceiveResult unknown = MsmqQueue.MoveReceiveResult.Unknown;
         do
         {
             using (MsmqEmptyMessage message = new MsmqEmptyMessage())
             {
                 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                 {
                     unknown = queue.TryReceiveByLookupId(messageProperty.LookupId, message, MsmqTransactionMode.CurrentOrThrow);
                     if ((MsmqQueue.MoveReceiveResult.Succeeded == unknown) && reject)
                     {
                         queue.MarkMessageRejected(messageProperty.LookupId);
                     }
                     scope.Complete();
                 }
             }
             if (unknown == MsmqQueue.MoveReceiveResult.Succeeded)
             {
                 MsmqDiagnostics.MessageConsumed(this.instanceId, messageProperty.MessageId, Msmq.IsRejectMessageSupported && reject);
             }
         }
         while ((unknown == MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction) && strategy.AnotherTryNeeded());
     }
     else
     {
         MsmqDiagnostics.MessageConsumed(this.instanceId, messageProperty.MessageId, false);
     }
 }
 internal void DropOrRejectReceivedMessage(MsmqQueue queue, MsmqMessageProperty messageProperty, bool reject)
 {
     if (this.Transactional)
     {
         TryAbortTransactionCurrent();
         IPostRollbackErrorStrategy  strategy = new SimplePostRollbackErrorStrategy(messageProperty.LookupId);
         MsmqQueue.MoveReceiveResult unknown  = MsmqQueue.MoveReceiveResult.Unknown;
         do
         {
             using (MsmqEmptyMessage message = new MsmqEmptyMessage())
             {
                 using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                 {
                     unknown = queue.TryReceiveByLookupId(messageProperty.LookupId, message, MsmqTransactionMode.CurrentOrThrow);
                     if ((MsmqQueue.MoveReceiveResult.Succeeded == unknown) && reject)
                     {
                         queue.MarkMessageRejected(messageProperty.LookupId);
                     }
                     scope.Complete();
                 }
             }
             if (unknown == MsmqQueue.MoveReceiveResult.Succeeded)
             {
                 MsmqDiagnostics.MessageConsumed(this.instanceId, messageProperty.MessageId, Msmq.IsRejectMessageSupported && reject);
             }
         }while ((unknown == MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction) && strategy.AnotherTryNeeded());
     }
     else
     {
         MsmqDiagnostics.MessageConsumed(this.instanceId, messageProperty.MessageId, false);
     }
 }
        private void InternalFinalDisposition(MsmqQueue disposeFromQueue, MsmqMessageProperty messageProperty)
        {
            switch (this.ReceiveParameters.ReceiveErrorHandling)
            {
                case ReceiveErrorHandling.Fault:
                    MsmqReceiveHelper.TryAbortTransactionCurrent();
                    if (this.receiver.ChannelListener != null)
                    {
                        this.receiver.ChannelListener.FaultListener();
                    }
                    if (this.receiver.Channel == null)
                    {
                        break;
                    }
                    this.receiver.Channel.FaultChannel();
                    return;

                case ReceiveErrorHandling.Drop:
                    this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, false);
                    return;

                case ReceiveErrorHandling.Reject:
                    this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, true);
                    MsmqDiagnostics.PoisonMessageRejected(messageProperty.MessageId, this.receiver.InstanceId);
                    return;

                case ReceiveErrorHandling.Move:
                    MsmqReceiveHelper.MoveReceivedMessage(disposeFromQueue, this.poisonQueue, messageProperty.LookupId);
                    MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, true, this.receiver.InstanceId);
                    break;

                default:
                    return;
            }
        }
示例#13
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();
        }
示例#14
0
 public MsmqSubqueueLockingQueue(string formatName, string hostname, int accessMode) : base(formatName, accessMode)
 {
     this.lockCollectionInterval = TimeSpan.FromMinutes(5.0);
     this.timerLock = new object();
     if (string.Compare(hostname, string.Empty, StringComparison.OrdinalIgnoreCase) == 0)
     {
         this.validHostName = TryGetHostName(formatName, out hostname);
     }
     else
     {
         this.validHostName = true;
     }
     this.disposed            = false;
     this.lockQueueName       = base.formatName + ";" + GenerateLockQueueName();
     this.lockQueueForReceive = new MsmqQueue(this.lockQueueName, 1, 1);
     this.lockQueueForMove    = new MsmqQueue(this.lockQueueName, 4);
     this.mainQueueForMove    = new MsmqQueue(base.formatName, 4);
     this.lockCollectionTimer = new IOThreadTimer(new Action <object>(this.OnCollectionTimer), null, false);
     if (string.Compare(hostname, "localhost", StringComparison.OrdinalIgnoreCase) == 0)
     {
         this.hostname = null;
     }
     else
     {
         this.hostname = hostname;
     }
 }
 public MsmqSubqueueLockingQueue(string formatName, string hostname, int accessMode) : base(formatName, accessMode)
 {
     this.lockCollectionInterval = TimeSpan.FromMinutes(5.0);
     this.timerLock = new object();
     if (string.Compare(hostname, string.Empty, StringComparison.OrdinalIgnoreCase) == 0)
     {
         this.validHostName = TryGetHostName(formatName, out hostname);
     }
     else
     {
         this.validHostName = true;
     }
     this.disposed = false;
     this.lockQueueName = base.formatName + ";" + GenerateLockQueueName();
     this.lockQueueForReceive = new MsmqQueue(this.lockQueueName, 1, 1);
     this.lockQueueForMove = new MsmqQueue(this.lockQueueName, 4);
     this.mainQueueForMove = new MsmqQueue(base.formatName, 4);
     this.lockCollectionTimer = new IOThreadTimer(new Action<object>(this.OnCollectionTimer), null, false);
     if (string.Compare(hostname, "localhost", StringComparison.OrdinalIgnoreCase) == 0)
     {
         this.hostname = null;
     }
     else
     {
         this.hostname = hostname;
     }
 }
示例#16
0
        private bool TryOpenLockQueueForCollection(string subqueueName, out MsmqQueue lockQueue)
        {
            lockQueue = null;
            string formatName = base.formatName + ";" + subqueueName;
            int    accessMode = 1;
            int    shareMode  = 1;

            try
            {
                int error = 0;
                if (MsmqQueue.IsQueueOpenable(formatName, accessMode, shareMode, out error))
                {
                    lockQueue = new MsmqQueue(formatName, accessMode, shareMode);
                    lockQueue.EnsureOpen();
                }
                else
                {
                    if ((error != -1072824311) && (error != -1072824317))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqOpenError", new object[] { MsmqError.GetErrorString(error) }), error));
                    }
                    return(false);
                }
            }
            catch (MsmqException)
            {
                return(false);
            }
            return(true);
        }
        public MsmqSubqueueLockingQueue(string formatName, string hostname, int accessMode)
            : base(formatName, accessMode)
        {
            // The hostname will be empty for MsmqIntegrationBinding
            if (string.Compare(hostname, string.Empty, StringComparison.OrdinalIgnoreCase) == 0)
            {
                this.validHostName = MsmqSubqueueLockingQueue.TryGetHostName(formatName, out hostname);
            }
            else
            {
                this.validHostName = true;
            }

            this.disposed            = false;
            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);
            this.mainQueueForMove    = new MsmqQueue(this.formatName, UnsafeNativeMethods.MQ_MOVE_ACCESS);
            this.lockCollectionTimer = new IOThreadTimer(new Action <object>(OnCollectionTimer), null, false);

            if (string.Compare(hostname, "localhost", StringComparison.OrdinalIgnoreCase) == 0)
            {
                this.hostname = null;
            }
            else
            {
                this.hostname = hostname;
            }
        }
        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;
                    }
                }
            }
        }
示例#19
0
 private void CloseQueue()
 {
     this.outputMessages.Dispose();
     if (this.msmqQueue != null)
     {
         this.msmqQueue.Dispose();
     }
     this.msmqQueue = null;
 }
 void CloseQueue()
 {
     this.outputMessages.Dispose();
     if (null != this.msmqQueue)
     {
         this.msmqQueue.Dispose();
     }
     this.msmqQueue = null;
 }
 private void CloseQueue()
 {
     this.outputMessages.Dispose();
     if (this.msmqQueue != null)
     {
         this.msmqQueue.Dispose();
     }
     this.msmqQueue = null;
 }
 public unsafe TryReceiveAsyncResult(MsmqQueue msmqQueue, NativeMsmqMessage message, TimeSpan timeout, int action, AsyncCallback callback, object state) : base(callback, state)
 {
     this.nativeOverlapped = null;
     this.msmqQueue        = msmqQueue;
     this.message          = message;
     this.action           = action;
     this.timeoutHelper    = new TimeoutHelper(timeout);
     this.StartReceive(true);
 }
 public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
 {
     this.queue = queue;
     this.receiveContextSettings = receiveContextSettings;
     this.messageExpiryMap = new Dictionary<long, MsmqReceiveContext>();
     this.transMessages = new Dictionary<Guid, List<MsmqReceiveContext>>();
     this.transactionCompletedHandler = new TransactionCompletedEventHandler(this.OnTransactionCompleted);
     this.messageExpiryTimer = new IOThreadTimer(new Action<object>(this.CleanupExpiredLocks), null, false);
     this.messageExpiryTimer.Set(this.messageTimeoutInterval);
 }
 public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
 {
     this.queue = queue;
     this.receiveContextSettings      = receiveContextSettings;
     this.messageExpiryMap            = new Dictionary <long, MsmqReceiveContext>();
     this.transMessages               = new Dictionary <Guid, List <MsmqReceiveContext> >();
     this.transactionCompletedHandler = new TransactionCompletedEventHandler(this.OnTransactionCompleted);
     this.messageExpiryTimer          = new IOThreadTimer(new Action <object>(this.CleanupExpiredLocks), null, false);
     this.messageExpiryTimer.Set(this.messageTimeoutInterval);
 }
示例#25
0
 static Msmq()
 {
     MsmqQueue.GetMsmqInformation(ref Msmq.version, ref activeDirectoryEnabled);
     MsmqDiagnostics.MsmqDetected(Msmq.version);
     System.Version version = Environment.OSVersion.Version;
     if ((version.Major == 5) && (version.Minor == 1))
     {
         xpSendLock = new object();
     }
 }
 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);
         }
     }
 }
示例#27
0
        static Msmq()
        {
            MsmqQueue.GetMsmqInformation(ref version, ref activeDirectoryEnabled);
            MsmqDiagnostics.MsmqDetected(version);
            Version osVersion = System.Environment.OSVersion.Version;

            if (osVersion.Major == 5 && osVersion.Minor == 1)
            {
                xpSendLock = new object();
            }
        }
示例#28
0
 internal MsmqReceiveHelper(MsmqReceiveParameters receiveParameters, Uri uri, IMsmqMessagePool messagePool, MsmqInputChannelBase channel, MsmqChannelListenerBase listener)
 {
     this.queueName         = receiveParameters.AddressTranslator.UriToFormatName(uri);
     this.receiveParameters = receiveParameters;
     this.uri           = uri;
     this.instanceId    = uri.ToString().ToUpperInvariant();
     this.pool          = messagePool;
     this.poisonHandler = Msmq.CreatePoisonHandler(this);
     this.channel       = channel;
     this.listener      = listener;
     this.queue         = Msmq.CreateMsmqQueue(this);
 }
示例#29
0
        internal static void VerifySender <TChannel>(MsmqChannelFactoryBase <TChannel> factory)
        {
            if (!factory.Durable && factory.ExactlyOnce)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqNoAssurancesForVolatile")));
            }
            MsmqChannelFactory <TChannel> factory2 = factory as MsmqChannelFactory <TChannel>;

            if (((factory2 != null) && factory2.UseActiveDirectory) && (factory2.QueueTransferProtocol != QueueTransferProtocol.Native))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqActiveDirectoryRequiresNativeTransfer")));
            }
            bool?useActiveDirectory = null;

            if (factory2 != null)
            {
                useActiveDirectory = new bool?(factory2.UseActiveDirectory);
            }
            VerifySecurity(factory.MsmqTransportSecurity, useActiveDirectory);
            if (null != factory.CustomDeadLetterQueue)
            {
                bool flag;
                if (DeadLetterQueue.Custom != factory.DeadLetterQueue)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqPerAppDLQRequiresCustom")));
                }
                if (!Msmq.IsPerAppDeadLetterQueueSupported)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqPerAppDLQRequiresMsmq4")));
                }
                if (!factory.ExactlyOnce)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqPerAppDLQRequiresExactlyOnce")));
                }
                string formatName = MsmqUri.NetMsmqAddressTranslator.UriToFormatName(factory.CustomDeadLetterQueue);
                if (!MsmqQueue.IsWriteable(formatName))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqDLQNotWriteable")));
                }
                if (!MsmqQueue.TryGetIsTransactional(formatName, out flag) || !flag)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqTransactedDLQExpected")));
                }
            }
            if ((null == factory.CustomDeadLetterQueue) && (DeadLetterQueue.Custom == factory.DeadLetterQueue))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqCustomRequiresPerAppDLQ")));
            }
            if (MsmqAuthenticationMode.Certificate == factory.MsmqTransportSecurity.MsmqAuthenticationMode)
            {
                EnsureSecurityTokenManagerPresent <TChannel>(factory);
            }
        }
 internal MsmqReceiveHelper(MsmqReceiveParameters receiveParameters, Uri uri, IMsmqMessagePool messagePool, MsmqInputChannelBase channel, MsmqChannelListenerBase listener)
 {
     this.queueName = receiveParameters.AddressTranslator.UriToFormatName(uri);
     this.receiveParameters = receiveParameters;
     this.uri = uri;
     this.instanceId = uri.ToString().ToUpperInvariant();
     this.pool = messagePool;
     this.poisonHandler = Msmq.CreatePoisonHandler(this);
     this.channel = channel;
     this.listener = listener;
     this.queue = Msmq.CreateMsmqQueue(this);
 }
        internal static void MoveReceivedMessage(MsmqQueue queueFrom, MsmqQueue queueTo, long lookupId)
        {
            TryAbortTransactionCurrent();
            IPostRollbackErrorStrategy strategy = new SimplePostRollbackErrorStrategy(lookupId);

            do
            {
                if (queueFrom.TryMoveMessage(lookupId, queueTo, MsmqTransactionMode.Single) != MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction)
                {
                    return;
                }
            }while (strategy.AnotherTryNeeded());
        }
 private void OnOpenCore(TimeSpan timeout)
 {
     if (null == Transaction.Current)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new InvalidOperationException(System.ServiceModel.SR.GetString("MsmqTransactionCurrentRequired")));
     }
     this.associatedTx = Transaction.Current;
     this.associatedTx.EnlistVolatile(new TransactionEnlistment(this, this.associatedTx), EnlistmentOptions.None);
     this.msmqQueue = new MsmqQueue(this.Factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri), 2);
     if (this.certificateTokenProvider != null)
     {
         this.certificateTokenProvider.Open(timeout);
     }
 }
        public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
        {
            Fx.Assert(queue is ILockingQueue, "Queue must be ILockingQueue");

            this.disposed = false;
            this.queue = queue;
            this.receiveContextSettings = receiveContextSettings;
            this.messageExpiryMap = new Dictionary<long, MsmqReceiveContext>();
            this.transMessages = new Dictionary<Guid, List<MsmqReceiveContext>>();
            transactionCompletedHandler = new TransactionCompletedEventHandler(OnTransactionCompleted);

            this.messageExpiryTimer = new IOThreadTimer(new Action<object>(CleanupExpiredLocks), null, false);
            this.messageExpiryTimer.Set(messageTimeoutInterval);
        }
        public MsmqReceiveContextLockManager(MsmqReceiveContextSettings receiveContextSettings, MsmqQueue queue)
        {
            Fx.Assert(queue is ILockingQueue, "Queue must be ILockingQueue");

            this.disposed = false;
            this.queue    = queue;
            this.receiveContextSettings = receiveContextSettings;
            this.messageExpiryMap       = new Dictionary <long, MsmqReceiveContext>();
            this.transMessages          = new Dictionary <Guid, List <MsmqReceiveContext> >();
            transactionCompletedHandler = new TransactionCompletedEventHandler(OnTransactionCompleted);

            this.messageExpiryTimer = new IOThreadTimer(new Action <object>(CleanupExpiredLocks), null, false);
            this.messageExpiryTimer.Set(messageTimeoutInterval);
        }
示例#35
0
 void OnOpenCore(TimeSpan timeout)
 {
     if (null == Transaction.Current)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new InvalidOperationException(SR.GetString(SR.MsmqTransactionCurrentRequired)));
     }
     this.associatedTx = Transaction.Current;
     this.associatedTx.EnlistVolatile(new TransactionEnlistment(this, this.associatedTx), EnlistmentOptions.None);
     this.msmqQueue = new MsmqQueue(this.Factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri),
                                    UnsafeNativeMethods.MQ_SEND_ACCESS);
     if (certificateTokenProvider != null)
     {
         certificateTokenProvider.Open(timeout);
     }
 }
示例#36
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);
        }
示例#37
0
        //
        internal static void MoveReceivedMessage(MsmqQueue queueFrom, MsmqQueue queueTo, long lookupId)
        {
            TryAbortTransactionCurrent();

            IPostRollbackErrorStrategy postRollback = new SimplePostRollbackErrorStrategy(lookupId);

            MsmqQueue.MoveReceiveResult result = MsmqQueue.MoveReceiveResult.Unknown;
            do
            {
                result = queueFrom.TryMoveMessage(lookupId, queueTo, MsmqTransactionMode.Single);

                if (result != MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction)
                {
                    break;
                }
            }while (postRollback.AnotherTryNeeded());
        }
            private void OnCompletion(int error, bool completedSynchronously)
            {
                Exception exception = null;

                this.receiveResult = MsmqQueue.ReceiveResult.MessageReceived;
                try
                {
                    if (error != 0)
                    {
                        if (error != -1072824293)
                        {
                            if (error != -1072824312)
                            {
                                if (!MsmqQueue.IsReceiveErrorDueToInsufficientBuffer(error))
                                {
                                    if (MsmqQueue.IsErrorDueToStaleHandle(error))
                                    {
                                        this.msmqQueue.HandleIsStale(this.handle);
                                    }
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqReceiveError", new object[] { MsmqError.GetErrorString(error) }), error));
                                }
                                this.message.Unpin();
                                this.message.GrowBuffers();
                                this.StartReceive(completedSynchronously);
                                return;
                            }
                            this.receiveResult = MsmqQueue.ReceiveResult.OperationCancelled;
                        }
                        else
                        {
                            this.receiveResult = MsmqQueue.ReceiveResult.Timeout;
                        }
                    }
                }
                catch (Exception exception2)
                {
                    if ((exception2 is NullReferenceException) || (exception2 is SEHException))
                    {
                        throw;
                    }
                    exception = exception2;
                }
                this.message.Unpin();
                base.Complete(completedSynchronously, exception);
            }
示例#39
0
 private void OpenQueue()
 {
     try
     {
         this.msmqQueue = new MsmqQueue(this.factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri), 2);
     }
     catch (MsmqException exception)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
     }
     if (this.factory.ExactlyOnce)
     {
         this.transactionMode = MsmqTransactionMode.CurrentOrSingle;
     }
     else
     {
         this.transactionMode = MsmqTransactionMode.None;
     }
 }
 private static bool SupportsAccessMode(string formatName, int accessType, out MsmqException msmqException)
 {
     msmqException = null;
     try
     {
         using (MsmqQueue queue = new MsmqQueue(formatName, accessType))
         {
             queue.GetHandle();
         }
     }
     catch (Exception exception)
     {
         msmqException = exception as MsmqException;
         if (msmqException == null)
         {
             throw;
         }
         return(false);
     }
     return(true);
 }
 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;
         }
     }
 }
示例#42
0
 public TryReceiveAsyncResult(MsmqQueue msmqQueue, NativeMsmqMessage message, TimeSpan timeout,
                              int action, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.msmqQueue = msmqQueue;
     this.message = message;
     this.action = action;
     this.timeoutHelper = new TimeoutHelper(timeout);
     StartReceive(true);
 }
示例#43
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 bool TryOpenLockQueueForCollection(string subqueueName, out MsmqQueue lockQueue)
        {
            lockQueue = null;
            string formatName = this.formatName + ";" + subqueueName;
            int accessMode = UnsafeNativeMethods.MQ_RECEIVE_ACCESS;
            int shareMode = UnsafeNativeMethods.MQ_DENY_RECEIVE_SHARE;

            try
            {
                int error = 0;
                if (MsmqQueue.IsQueueOpenable(formatName, accessMode, shareMode, out error))
                {
                    lockQueue = new MsmqQueue(formatName, accessMode, shareMode);
                    lockQueue.EnsureOpen();
                }
                else
                {
                    // The lock subqueue is either being actively used by a channel or is not available.
                    // So, we do not have to collect this lock queue.
                    if (error == UnsafeNativeMethods.MQ_ERROR_SHARING_VIOLATION ||
                        error == UnsafeNativeMethods.MQ_ERROR_QUEUE_NOT_FOUND)
                    {
                        return false;
                    }
                    else
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(SR.GetString(SR.MsmqOpenError, MsmqError.GetErrorString(error)), error));
                    }
                }
            }
            catch (MsmqException)
            {
                // The error has already been logged. Since this function is to decide whether to collect
                // the lock queue, we return false.
                return false;
            }

            return true;
        }
        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);
            }
        }
 void OnOpenCore(TimeSpan timeout)
 {
     if (null == Transaction.Current)
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperCritical(new InvalidOperationException(SR.GetString(SR.MsmqTransactionCurrentRequired)));
     this.associatedTx = Transaction.Current;
     this.associatedTx.EnlistVolatile(new TransactionEnlistment(this, this.associatedTx), EnlistmentOptions.None);
     this.msmqQueue = new MsmqQueue(this.Factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri),
                                    UnsafeNativeMethods.MQ_SEND_ACCESS);
     if (certificateTokenProvider != null)
     {
         certificateTokenProvider.Open(timeout);
     }
 }
 public void Open()
 {
     if (this.ReceiveParameters.ReceiveContextSettings.Enabled)
     {
         this.lockQueueForReceive = ((MsmqSubqueueLockingQueue) this.receiver.Queue).LockQueueForReceive;
     }
     this.mainQueue = this.receiver.Queue;
     this.mainQueueForMove = new MsmqQueue(this.mainQueueName, 4);
     this.poisonQueue = new MsmqQueue(this.poisonQueueName, 4);
     this.retryQueueForMove = new MsmqQueue(this.retryQueueName, 4);
     this.retryQueueForPeek = new MsmqQueue(this.retryQueueName, 1);
     this.retryQueueMessage = new MsmqRetryQueueMessage();
     if (Thread.CurrentThread.IsThreadPoolThread)
     {
         StartPeek(this);
     }
     else
     {
         ActionItem.Schedule(onStartPeek, this);
     }
 }
        //
        internal static void MoveReceivedMessage(MsmqQueue queueFrom, MsmqQueue queueTo, long lookupId)
        {
            TryAbortTransactionCurrent();

            IPostRollbackErrorStrategy postRollback = new SimplePostRollbackErrorStrategy(lookupId);
            MsmqQueue.MoveReceiveResult result = MsmqQueue.MoveReceiveResult.Unknown;
            do
            {
                result = queueFrom.TryMoveMessage(lookupId, queueTo, MsmqTransactionMode.Single);

                if (result != MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction)
                    break;
            }
            while (postRollback.AnotherTryNeeded());
        }
 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();
 }
 internal static void MoveReceivedMessage(MsmqQueue queueFrom, MsmqQueue queueTo, long lookupId)
 {
     TryAbortTransactionCurrent();
     IPostRollbackErrorStrategy strategy = new SimplePostRollbackErrorStrategy(lookupId);
     do
     {
         if (queueFrom.TryMoveMessage(lookupId, queueTo, MsmqTransactionMode.Single) != MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction)
         {
             return;
         }
     }
     while (strategy.AnotherTryNeeded());
 }
 private bool TryOpenLockQueueForCollection(string subqueueName, out MsmqQueue lockQueue)
 {
     lockQueue = null;
     string formatName = base.formatName + ";" + subqueueName;
     int accessMode = 1;
     int shareMode = 1;
     try
     {
         int error = 0;
         if (MsmqQueue.IsQueueOpenable(formatName, accessMode, shareMode, out error))
         {
             lockQueue = new MsmqQueue(formatName, accessMode, shareMode);
             lockQueue.EnsureOpen();
         }
         else
         {
             if ((error != -1072824311) && (error != -1072824317))
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MsmqException(System.ServiceModel.SR.GetString("MsmqOpenError", new object[] { MsmqError.GetErrorString(error) }), error));
             }
             return false;
         }
     }
     catch (MsmqException)
     {
         return false;
     }
     return true;
 }
示例#52
0
        static bool SupportsAccessMode(string formatName, int accessType, out MsmqException msmqException)
        {
            msmqException = null;

            try
            {
                using (MsmqQueue msmqQueue = new MsmqQueue(formatName, accessType))
                {
                    msmqQueue.GetHandle();
                }
            }
            catch (Exception ex)
            {
                msmqException = ex as MsmqException;
                if (null != msmqException)
                {
                    return false;
                }
                throw;
            }
            return true;
        }
        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();
        }
        internal void DropOrRejectReceivedMessage(MsmqQueue queue, MsmqMessageProperty messageProperty, bool reject)
        {
            if (this.Transactional)
            {
                TryAbortTransactionCurrent();
                IPostRollbackErrorStrategy postRollback = new SimplePostRollbackErrorStrategy(messageProperty.LookupId);
                MsmqQueue.MoveReceiveResult result = MsmqQueue.MoveReceiveResult.Unknown;
                do
                {
                    using (MsmqEmptyMessage emptyMessage = new MsmqEmptyMessage())
                    {
                        using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                        {
                            result = queue.TryReceiveByLookupId(messageProperty.LookupId, emptyMessage, MsmqTransactionMode.CurrentOrThrow);
                            if (MsmqQueue.MoveReceiveResult.Succeeded == result && reject)
                                queue.MarkMessageRejected(messageProperty.LookupId);
                            scope.Complete();
                        }
                    }

                    if (result == MsmqQueue.MoveReceiveResult.Succeeded)
                        // If 'Reject' supported and 'Reject' requested, put reject in the trace, otherwise put 'Drop'
                        MsmqDiagnostics.MessageConsumed(instanceId, messageProperty.MessageId, (Msmq.IsRejectMessageSupported && reject));

                    if (result != MsmqQueue.MoveReceiveResult.MessageLockedUnderTransaction)
                        break;
                }
                while (postRollback.AnotherTryNeeded());
            }
            else
            {
                MsmqDiagnostics.MessageConsumed(instanceId, messageProperty.MessageId, false);
            }
        }
        private void InternalFinalDisposition(MsmqQueue disposeFromQueue, MsmqMessageProperty messageProperty)
        {
            switch (this.ReceiveParameters.ReceiveErrorHandling)
            {
                case ReceiveErrorHandling.Drop:
                    this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, false);
                    break;

                case ReceiveErrorHandling.Fault:
                    MsmqReceiveHelper.TryAbortTransactionCurrent();
                    if (null != this.receiver.ChannelListener)
                        this.receiver.ChannelListener.FaultListener();
                    if (null != this.receiver.Channel)
                        this.receiver.Channel.FaultChannel();
                    break;

                case ReceiveErrorHandling.Reject:
                    this.receiver.DropOrRejectReceivedMessage(disposeFromQueue, messageProperty, true);
                    MsmqDiagnostics.PoisonMessageRejected(messageProperty.MessageId, this.receiver.InstanceId);
                    break;

                case ReceiveErrorHandling.Move:
                    MsmqReceiveHelper.MoveReceivedMessage(disposeFromQueue, this.poisonQueue, messageProperty.LookupId);
                    MsmqDiagnostics.PoisonMessageMoved(messageProperty.MessageId, true, this.receiver.InstanceId);
                    break;

                default:
                    Fx.Assert("System.ServiceModel.Channels.Msmq4PoisonHandler.FinalDisposition(): (unexpected ReceiveErrorHandling)");
                    break;
            }
        }
        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;
                    }
                }
            }
        }
 public WaitForMessageAsyncResult(MsmqQueue msmqQueue, TimeSpan timeout, AsyncCallback callback, object state)
     : base(callback, state)
 {
     this.msmqMessage = new MsmqEmptyMessage();
     this.msmqQueue = msmqQueue;
     this.msmqQueue.BeginPeek(this.msmqMessage, timeout, onCompleteStatic, this);
 }
 void OpenQueue()
 {
     try
     {
         this.msmqQueue = new MsmqQueue(this.factory.AddressTranslator.UriToFormatName(this.RemoteAddress.Uri),
                                    UnsafeNativeMethods.MQ_SEND_ACCESS);
     }
     catch (MsmqException ex)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
     }
     if (this.factory.ExactlyOnce)
     {
         this.transactionMode = MsmqTransactionMode.CurrentOrSingle;
     }
     else
     {
         this.transactionMode = MsmqTransactionMode.None;
     }
 }