Esempio n. 1
0
        /// <summary>
        /// Attempts to refresh the stored <see cref="IServiceBusNamespace"/> fluent construct.
        ///     Will do a full rebuild if any type of failure occurs during the refresh.
        /// </summary>
        /// <returns>The refreshed <see cref="IServiceBusNamespace"/>.</returns>
        internal async Task <IServiceBusNamespace> GetRefreshedServiceBusNamespace()
        {
            try
            {
                if (AzureServiceBusNamespace != null)
                {
                    return(await AzureServiceBusNamespace.RefreshAsync().ConfigureAwait(false));
                }
            }
            catch { /* soak */ }

            var token            = await new AzureServiceTokenProvider().GetAccessTokenAsync("https://management.core.windows.net/", string.Empty).ConfigureAwait(false);
            var tokenCredentials = new TokenCredentials(token);

            var client = RestClient.Configure()
                         .WithEnvironment(AzureEnvironment.AzureGlobalCloud)
                         .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                         .WithCredentials(new AzureCredentials(tokenCredentials, tokenCredentials, string.Empty, AzureEnvironment.AzureGlobalCloud))
                         .Build();

            AzureServiceBusNamespace = (await Azure.Authenticate(client, string.Empty)
                                        .WithSubscription(SubscriptionId)
                                        .ServiceBusNamespaces.ListAsync()
                                        .ConfigureAwait(false))
                                       .SingleOrDefault(n => n.Name == NamespaceName);

            if (AzureServiceBusNamespace == null)
            {
                throw new InvalidOperationException($"Couldn't find the service bus namespace {NamespaceName} in the subscription with ID {SubscriptionId}");
            }

            return(AzureServiceBusNamespace);
        }
Esempio n. 2
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)
        {
            await sbNamespace.RefreshAsync();

            var topic = (await sbNamespace.Topics.ListAsync()).SingleOrDefault(t => t.Name == name.ToLowerInvariant());

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

            await sbNamespace.Topics
            .Define(name.ToLowerInvariant())
            .WithDuplicateMessageDetection(TimeSpan.FromMinutes(10))
            .CreateAsync();

            await sbNamespace.RefreshAsync();

            return((await sbNamespace.Topics.ListAsync()).Single(t => t.Name == name.ToLowerInvariant()));
        }