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

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

            try
            {
                return(await sbNamespace.Topics
                       .Define(name.ToLowerInvariant())
                       .WithDuplicateMessageDetection(TimeSpan.FromMinutes(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 Topic cannot be changed"))
                {
                    // Create topic race condition occurred. Return existing topic.
                    return(await sbNamespace.GetTopicByNameAsync(name));
                }
        }