public async Task SubscribeAll(MessageMetadata[] eventTypes, ContextBag context, CancellationToken cancellationToken = default)
        {
            await namespacePermissions.CanManage(cancellationToken).ConfigureAwait(false);

            foreach (var eventType in eventTypes)
            {
                await SubscribeEvent(administrativeClient, eventType.MessageType, cancellationToken).ConfigureAwait(false);
            }
        }
        async Task CheckForManagePermissions()
        {
            if (startupCheckResult == null)
            {
                startupCheckResult = await namespacePermissions.CanManage().ConfigureAwait(false);
            }

            if (!startupCheckResult.Succeeded)
            {
                throw new Exception(startupCheckResult.ErrorMessage);
            }
        }
        public async Task CreateQueues(string[] queues, CancellationToken cancellationToken = default)
        {
            await namespacePermissions.CanManage(cancellationToken).ConfigureAwait(false);

            var topic = new CreateTopicOptions(transportSettings.TopicName)
            {
                EnableBatchedOperations = true,
                EnablePartitioning      = transportSettings.EnablePartitioning,
                MaxSizeInMegabytes      = maxSizeInMb
            };

            try
            {
                await administrativeClient.CreateTopicAsync(topic, cancellationToken).ConfigureAwait(false);
            }
            catch (ServiceBusException sbe) when(sbe.Reason == ServiceBusFailureReason.MessagingEntityAlreadyExists)
            {
                Logger.Info($"Topic {topic.Name} already exists");
            }
            catch (ServiceBusException sbe) when(sbe.IsTransient) // An operation is in progress.
            {
                Logger.Info($"Topic creation for {topic.Name} is already in progress");
            }

            foreach (var address in queues)
            {
                var queue = new CreateQueueOptions(address)
                {
                    EnableBatchedOperations = true,
                    LockDuration            = TimeSpan.FromMinutes(5),
                    MaxDeliveryCount        = int.MaxValue,
                    MaxSizeInMegabytes      = maxSizeInMb,
                    EnablePartitioning      = transportSettings.EnablePartitioning
                };

                try
                {
                    await administrativeClient.CreateQueueAsync(queue, cancellationToken).ConfigureAwait(false);
                }
                catch (ServiceBusException sbe) when(sbe.Reason == ServiceBusFailureReason.MessagingEntityAlreadyExists)
                {
                    Logger.Debug($"Queue {queue.Name} already exists");
                }
                catch (ServiceBusException sbe) when(sbe.IsTransient) // An operation is in progress.
                {
                    Logger.Info($"Queue creation for {queue.Name} is already in progress");
                }
            }
        }
        public async Task CreateQueueIfNecessary(QueueBindings queueBindings, string identity)
        {
            var result = await namespacePermissions.CanManage().ConfigureAwait(false);

            if (!result.Succeeded)
            {
                throw new Exception(result.ErrorMessage);
            }

            var client = new ManagementClient(connectionStringBuilder, tokenProvider);

            var topic = new TopicDescription(topicName)
            {
                EnableBatchedOperations = true,
                EnablePartitioning      = enablePartitioning,
                MaxSizeInMB             = maxSizeInMB
            };

            try
            {
                await client.CreateTopicAsync(topic).ConfigureAwait(false);
            }
            catch (MessagingEntityAlreadyExistsException)
            {
            }
            // TODO: refactor when https://github.com/Azure/azure-service-bus-dotnet/issues/525 is fixed
            catch (ServiceBusException sbe) when(sbe.Message.Contains("SubCode=40901."))  // An operation is in progress.
            {
            }

            foreach (var address in queueBindings.ReceivingAddresses.Concat(queueBindings.SendingAddresses))
            {
                var queue = new QueueDescription(address)
                {
                    EnableBatchedOperations = true,
                    LockDuration            = TimeSpan.FromMinutes(5),
                    MaxDeliveryCount        = int.MaxValue,
                    MaxSizeInMB             = maxSizeInMB,
                    EnablePartitioning      = enablePartitioning
                };

                try
                {
                    await client.CreateQueueAsync(queue).ConfigureAwait(false);
                }
                catch (MessagingEntityAlreadyExistsException)
                {
                }
                // TODO: refactor when https://github.com/Azure/azure-service-bus-dotnet/issues/525 is fixed
                catch (ServiceBusException sbe) when(sbe.Message.Contains("SubCode=40901."))  // An operation is in progress.
                {
                }
            }

            var subscriptionName = mainInputQueueName.Length > maxNameLength?subscriptionShortener(mainInputQueueName) : mainInputQueueName;

            var subscription = new SubscriptionDescription(topicName, subscriptionName)
            {
                LockDuration = TimeSpan.FromMinutes(5),
                ForwardTo    = mainInputQueueName,
                EnableDeadLetteringOnFilterEvaluationExceptions = false,
                MaxDeliveryCount        = int.MaxValue,
                EnableBatchedOperations = true,
                UserMetadata            = mainInputQueueName
            };

            try
            {
                await client.CreateSubscriptionAsync(subscription, new RuleDescription("$default", new FalseFilter())).ConfigureAwait(false);
            }
            catch (MessagingEntityAlreadyExistsException)
            {
            }
            // TODO: refactor when https://github.com/Azure/azure-service-bus-dotnet/issues/525 is fixed
            catch (ServiceBusException sbe) when(sbe.Message.Contains("SubCode=40901."))  // An operation is in progress.
            {
            }

            await client.CloseAsync().ConfigureAwait(false);
        }