コード例 #1
0
        public static async Task <IConnection> Connect(IEnumerable <string> hostnames,
                                                       string vhost    = "/", string username = "******",
                                                       string password = "******", int port    = 5672,
                                                       AutoRecoverySettings recoverySettings = null, string connectionName = null,
                                                       int maxChannels = 30, ushort heartbeat = 0)
        {
            recoverySettings = recoverySettings ?? AutoRecoverySettings.Off;
            connectionName   = connectionName ?? DefaultConnectionName;

            var conn = new Connection();

            try
            {
                foreach (var hostname in hostnames)
                {
                    var successful =
                        await conn.Connect(hostname, vhost,
                                           username, password, port, connectionName, heartbeat,
                                           throwOnError : false).ConfigureAwait(false);

                    if (successful)
                    {
                        LogAdapter.LogWarn(LogSource, "Selected " + hostname);

                        conn.SetMaxChannels(maxChannels);

                        return(recoverySettings.Enabled ?
                               (IConnection) new RecoveryEnabledConnection(hostnames, conn, recoverySettings) :
                               conn);
                    }
                }

                // TODO: collect exceptions and add them to aggregateexception:
                throw new AggregateException("Could not connect to any of the provided hosts");
            }
            catch (Exception e)
            {
                if (LogAdapter.IsErrorEnabled)
                {
                    LogAdapter.LogError(LogSource, "Connection error", e);
                }

                conn.Dispose();
                throw;
            }
        }
コード例 #2
0
        public static async Task <IConnection> Connect(string hostname,
                                                       string vhost    = "/", string username = "******",
                                                       string password = "******", int port    = 5672,
                                                       AutoRecoverySettings recoverySettings = null, string connectionName = null,
                                                       int maxChannels = 30, ushort heartbeat = 0)
        {
            recoverySettings = recoverySettings ?? AutoRecoverySettings.Off;
            connectionName   = connectionName ?? DefaultConnectionName;

            var conn = new Connection();

            try
            {
                await conn
                .Connect(hostname, vhost, username, password, port, connectionName, heartbeat, throwOnError : true)
                .ConfigureAwait(false);

                conn.SetMaxChannels(maxChannels);

                if (LogAdapter.ExtendedLogEnabled)
                {
                    LogAdapter.LogDebug(LogSource, "Connected to " + hostname + ":" + port);
                }

                return(recoverySettings.Enabled ? (IConnection) new RecoveryEnabledConnection(hostname, conn, recoverySettings) : conn);
            }
            catch (Exception e)
            {
                if (LogAdapter.IsErrorEnabled)
                {
                    LogAdapter.LogError(LogSource, "Connection error: " + hostname + ":" + port, e);
                }

                conn.Dispose();
                throw;
            }
        }