Contour.Transport.RabbitMQ.Internal.RabbitBus.Connect C# (CSharp) Method

Connect() private method

private Connect ( CancellationToken token ) : void
token System.Threading.CancellationToken
return void
        private void Connect(CancellationToken token)
        {
            this.logger.InfoFormat("Connecting to RabbitMQ using [{0}].", this.Configuration.ConnectionString);

            var clientProperties = new Dictionary<string, object>
                                       {
                                           { "Endpoint", this.Endpoint.Address },
                                           { "Machine", Environment.MachineName },
                                           {
                                               "Location", Path.GetDirectoryName(
                                                   Assembly.GetExecutingAssembly()
                                               .
                                               CodeBase)
                                           }
                                       };

            var connectionFactory = new ConnectionFactory
                                        {
                                            Uri = this.Configuration.ConnectionString,
                                            ClientProperties = clientProperties,
                                            RequestedConnectionTimeout = 3000 // 3s
                                        };

            var retryCount = 0;
            while (!token.IsCancellationRequested)
            {
                IConnection newConnection = null;
                try
                {
                    newConnection = connectionFactory.CreateConnection();
                    newConnection.ConnectionShutdown += this.DisconnectEventHandler;
                    this.Connection = newConnection;
                    this.OnConnected();
                    return;
                }
                catch (Exception ex)
                {
                    var secondsToRetry = Math.Min(10, retryCount);

                    this.logger.WarnFormat("Unable to connect to RabbitMQ. Retrying in {0} seconds...", ex, secondsToRetry);

                    if (newConnection != null)
                    {
                        newConnection.ConnectionShutdown -= this.DisconnectEventHandler;
                        newConnection.Abort(500);
                    }

                    Thread.Sleep(TimeSpan.FromSeconds(secondsToRetry));
                    retryCount++;
                }
            }
        }