Beispiel #1
0
 public MsmqAddress MakeCompatibleWith(MsmqAddress other, Func<string, string> ipLookup)
 {
     IPAddress _;
     if (IPAddress.TryParse(other.Machine, out _) && !IPAddress.TryParse(Machine, out _))
     {
         return new MsmqAddress(Queue, ipLookup(Machine));
     }
     return this;
 }
 public WorkerFeature()
 {
     DependsOn<DelayedDeliveryFeature>();
     Defaults(s =>
     {
         var distributorAddress = s.Get<string>("LegacyDistributor.Address");
         var distributorMsmqAddress = MsmqAddress.Parse(distributorAddress);
         var distributorTimeoutQueue = new MsmqAddress(distributorMsmqAddress.Queue + ".Timeouts", distributorMsmqAddress.Machine);
         var timeoutManagerAddressConfiguration = s.Get<TimeoutManagerAddressConfiguration>();
         timeoutManagerAddressConfiguration.Set(distributorTimeoutQueue.ToString());
     });
 }
        void CreateQueueIfNecessary(string address, string identity)
        {
            var msmqAddress = MsmqAddress.Parse(address);

            Logger.Debug($"Creating '{address}' if needed.");

            if (msmqAddress.IsRemote)
            {
                Logger.Info($"'{address}' is a remote queue and won't be created");
                return;
            }

            var queuePath = msmqAddress.PathWithoutPrefix;

            if (MessageQueue.Exists(queuePath))
            {
                Logger.Debug($"'{address}' already exists");
                return;
            }

            try
            {
                using (var queue = MessageQueue.Create(queuePath, useTransactionalQueues))
                {
                    Logger.Debug($"Created queue, path: [{queuePath}], identity: [{identity}], transactional: [{useTransactionalQueues}]");

                    try
                    {
                        queue.SetPermissions(identity, MessageQueueAccessRights.WriteMessage);
                        queue.SetPermissions(identity, MessageQueueAccessRights.ReceiveMessage);
                        queue.SetPermissions(identity, MessageQueueAccessRights.PeekMessage);

                        queue.SetPermissions(LocalAdministratorsGroupName, MessageQueueAccessRights.FullControl);
                    }
                    catch (MessageQueueException permissionException) when(permissionException.MessageQueueErrorCode == MessageQueueErrorCode.FormatNameBufferTooSmall)
                    {
                        Logger.Warn($"The name for queue '{queue.FormatName}' is too long for permissions to be applied. Please consider a shorter endpoint name.", permissionException);
                    }
                }
            }
            catch (MessageQueueException ex) when(ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueExists)
            {
                //Solves the race condition problem when multiple endpoints try to create same queue (e.g. error queue).
            }
        }
Beispiel #4
0
        public MsmqSubscriptionStorageQueue(MsmqAddress queueAddress)
        {
            q = new MessageQueue(queueAddress.FullPath);

            var messageReadPropertyFilter = new MessagePropertyFilter
            {
                Id          = true,
                Body        = true,
                Label       = true,
                ArrivedTime = true
            };

            q.Formatter = new XmlMessageFormatter(new[]
            {
                typeof(string)
            });

            q.MessageReadPropertyFilter = messageReadPropertyFilter;
        }
        public MsmqSubscriptionStorageQueue(MsmqAddress queueAddress, bool useTransactionalQueue)
        {
            transactionTypeToUseForSend = useTransactionalQueue ? MessageQueueTransactionType.Single : MessageQueueTransactionType.None;
            queue = new MessageQueue(queueAddress.FullPath);

            var messageReadPropertyFilter = new MessagePropertyFilter
            {
                Id = true,
                Body = true,
                Label = true,
                ArrivedTime = true
            };

            queue.Formatter = new XmlMessageFormatter(new[]
            {
                typeof(string)
            });

            queue.MessageReadPropertyFilter = messageReadPropertyFilter;
        }
Beispiel #6
0
        public MsmqSubscriptionStorageQueue(MsmqAddress queueAddress, bool useTransactionalQueue)
        {
            transactionTypeToUseForSend = useTransactionalQueue ? MessageQueueTransactionType.Single : MessageQueueTransactionType.None;
            queue = new MessageQueue(queueAddress.FullPath);

            var messageReadPropertyFilter = new MessagePropertyFilter
            {
                Id          = true,
                Body        = true,
                Label       = true,
                ArrivedTime = true
            };

            queue.Formatter = new XmlMessageFormatter(new[]
            {
                typeof(string)
            });

            queue.MessageReadPropertyFilter = messageReadPropertyFilter;
        }
        public static bool TryOpenQueue(MsmqAddress msmqAddress, out MessageQueue messageQueue)
        {
            messageQueue = null;

            var queuePath = msmqAddress.PathWithoutPrefix;

            Logger.Debug($"Checking if queue exists: {queuePath}.");

            if (msmqAddress.IsRemote)
            {
                Logger.Debug("Queue is on remote machine.");
                Logger.Debug("If this does not succeed (like if the remote machine is disconnected), processing will continue.");
            }

            var path = msmqAddress.PathWithoutPrefix;

            try
            {
                if (MessageQueue.Exists(path))
                {
                    messageQueue = new MessageQueue(path);

                    Logger.DebugFormat("Verified that the queue: [{0}] existed", queuePath);

                    return(true);
                }
            }
            catch (MessageQueueException)
            {
                // Can happen because of an invalid queue path or trying to access a remote private queue.
                // Either way, this results in a failed attempt, therefore returning false.

                return(false);
            }

            return(false);
        }
Beispiel #8
0
 public override string MakeCanonicalForm(string transportAddress)
 {
     return(MsmqAddress.Parse(transportAddress).ToString());
 }
Beispiel #9
0
        void ExecuteTransportOperation(TransportTransaction transaction, UnicastTransportOperation transportOperation)
        {
            var message = transportOperation.Message;

            var destination        = transportOperation.Destination;
            var destinationAddress = MsmqAddress.Parse(destination);

            if (IsCombiningTimeToBeReceivedWithTransactions(
                    transaction,
                    transportOperation.RequiredDispatchConsistency,
                    transportOperation.DeliveryConstraints))
            {
                throw new Exception($"Failed to send message to address: {destinationAddress.Queue}@{destinationAddress.Machine}. Sending messages with a custom TimeToBeReceived is not supported on transactional MSMQ.");
            }

            try
            {
                using (var q = new MessageQueue(destinationAddress.FullPath, false, settings.UseConnectionCache, QueueAccessMode.Send))
                {
                    using (var toSend = MsmqUtilities.Convert(message, transportOperation.DeliveryConstraints))
                    {
                        toSend.UseDeadLetterQueue = settings.UseDeadLetterQueue;
                        toSend.UseJournalQueue    = settings.UseJournalQueue;
                        toSend.TimeToReachQueue   = settings.TimeToReachQueue;

                        string replyToAddress;

                        if (message.Headers.TryGetValue(Headers.ReplyToAddress, out replyToAddress))
                        {
                            toSend.ResponseQueue = new MessageQueue(MsmqAddress.Parse(replyToAddress).FullPath);
                        }

                        var label = GetLabel(message);

                        if (transportOperation.RequiredDispatchConsistency == DispatchConsistency.Isolated)
                        {
                            q.Send(toSend, label, GetIsolatedTransactionType());
                            return;
                        }

                        MessageQueueTransaction activeTransaction;
                        if (TryGetNativeTransaction(transaction, out activeTransaction))
                        {
                            q.Send(toSend, label, activeTransaction);
                            return;
                        }

                        q.Send(toSend, label, GetTransactionTypeForSend());
                    }
                }
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.QueueNotFound)
                {
                    var msg = destination == null
                        ? "Failed to send message. Target address is null."
                        : $"Failed to send message to address: [{destination}]";

                    throw new QueueNotFoundException(destination, msg, ex);
                }

                ThrowFailedToSendException(destination, ex);
            }
            catch (Exception ex)
            {
                ThrowFailedToSendException(destination, ex);
            }
        }