예제 #1
0
        /// <summary>
        /// Creates a specific queue if it doesn't exist in the target namespace.
        /// </summary>
        /// <param name="sbNamespace">The <see cref="IServiceBusNamespace"/> where we are creating the queue in.</param>
        /// <param name="name">The name of the queue that we are looking for.</param>
        /// <returns>The <see cref="IQueue"/> entity object that references the Azure queue.</returns>
        public static async Task <IQueue> CreateQueueIfNotExists(this IServiceBusNamespace sbNamespace, string name)
        {
            var queue = await sbNamespace.GetQueueByNameAsync(name);

            if (queue != null)
            {
                return(queue);
            }

            try
            {
                return(await sbNamespace.Queues
                       .Define(name.ToLowerInvariant())
                       .WithMessageLockDurationInSeconds(60)
                       .WithDuplicateMessageDetection(TimeSpan.FromMinutes(10))
                       .WithExpiredMessageMovedToDeadLetterQueue()
                       .WithMessageMovedToDeadLetterQueueOnMaxDeliveryCount(10)
                       .CreateAsync());
            }
            catch (CloudException ce)
                when(ce.Response.StatusCode == HttpStatusCode.BadRequest &&
                     ce.Message.Contains("SubCode=40000. The value for the requires duplicate detection property of an existing Queue cannot be changed"))
                {
                    // Create queue race condition occurred. Return existing queue.
                    return(await sbNamespace.GetQueueByNameAsync(name));
                }
        }