private async Task CreateTopic(ServiceBusConnectionContext connectionContext, int topicIndex)
        {
            await ServiceBusConnectionContext.TopicClientsLock.WaitAsync();

            try
            {
                if (connectionContext.IsDisposed)
                {
                    return;
                }

                string topicName = connectionContext.TopicNames[topicIndex];

                if (!await _namespaceManager.TopicExistsAsync(topicName))
                {
                    try
                    {
                        _trace.TraceInformation("Creating a new topic {0} in the service bus...", topicName);

                        await _namespaceManager.CreateTopicAsync(topicName);

                        _trace.TraceInformation(
                            "Creation of a new topic {0} in the service bus completed successfully.", topicName);
                    }
                    catch (MessagingEntityAlreadyExistsException)
                    {
                        // The entity already exists
                        _trace.TraceInformation(
                            "Creation of a new topic {0} threw an MessagingEntityAlreadyExistsException.", topicName);
                    }
                }

                // Create a client for this topic
                TopicClient topicClient = new TopicClient(_connectionString, topicName, _configuration.RetryPolicy ?? RetryExponential.Default);

                //if (_configuration.RetryPolicy != null)
                //{
                //    topicClient.RetryPolicy = ;
                //}
                //else
                //{
                //    topicClient.RetryPolicy = .RetryPolicy = ;
                //}

                connectionContext.SetTopicClients(topicClient, topicIndex);

                _trace.TraceInformation("Creation of a new topic client {0} completed successfully.", topicName);
            }
            finally
            {
                ServiceBusConnectionContext.TopicClientsLock.Release();
            }

            //CreateSubscription(connectionContext, topicIndex);
        }
        public async Task Subscribe(ServiceBusConnectionContext connectionContext)
        {
            if (connectionContext == null)
            {
                throw new ArgumentNullException("connectionContext");
            }

            _trace.TraceInformation("Subscribing to {0} topic(s) in the service bus...", connectionContext.TopicNames.Count);

            connectionContext.NamespaceManager = _namespaceManager;

            for (var topicIndex = 0; topicIndex < connectionContext.TopicNames.Count; ++topicIndex)
            {
                await Retry(() => CreateTopic(connectionContext, topicIndex));
            }

            _trace.TraceInformation("Subscription to {0} topics in the service bus Topic service completed successfully.", connectionContext.TopicNames.Count);
        }
Beispiel #3
0
        public ServiceBusMessageBus(IDependencyResolver resolver, ServiceBusScaleoutConfiguration configuration)
            : base(resolver, configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            // Retrieve the trace manager
            var traceManager = resolver.Resolve <ITraceManager>();

            _trace = traceManager["SignalR." + nameof(ServiceBusMessageBus)];

            _connection = new ServiceBusConnection(configuration, _trace);

            _topics = Enumerable.Range(0, configuration.TopicCount)
                      .Select(topicIndex => SignalRTopicPrefix + "_" + configuration.TopicPrefix + "_" + topicIndex)
                      .ToArray();

            _connectionContext = new ServiceBusConnectionContext(configuration, _topics, _trace, OnMessage, OnError, Open);

            Subscribe().GetAwaiter().GetResult();
        }