/// <summary> /// Opens a connection to the specified address. /// </summary> /// <param name="addressUri">The address Uri. User info is ignored.</param> /// <param name="saslHandler">The SASL handler which determines the SASL mechanism. Null means no SASL handshake.</param> /// <param name="timeout">The operation timeout.</param> /// <returns>An AMQP connection.</returns> public async Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, SaslHandler saslHandler, TimeSpan timeout) { TransportSettings transportSettings; if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase)) { transportSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultPort }; } else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase)) { TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultSecurePort }; var tls = new TlsTransportSettings(tcpSettings) { TargetHost = addressUri.Host }; TlsTransportProvider tlsProvider = this.settings.GetTransportProvider <TlsTransportProvider>(); if (tlsProvider != null) { tls.CertificateValidationCallback = tlsProvider.Settings.CertificateValidationCallback; tls.CheckCertificateRevocation = tlsProvider.Settings.CheckCertificateRevocation; tls.Certificate = tlsProvider.Settings.Certificate; tls.Protocols = tlsProvider.Settings.Protocols; } transportSettings = tls; } else if (addressUri.Scheme.Equals(WebSocketTransportSettings.WebSockets, StringComparison.OrdinalIgnoreCase) || addressUri.Scheme.Equals(WebSocketTransportSettings.SecureWebSockets, StringComparison.OrdinalIgnoreCase)) { transportSettings = new WebSocketTransportSettings() { Uri = addressUri }; } else { throw new NotSupportedException(addressUri.Scheme); } AmqpSettings settings = this.settings.Clone(); settings.TransportProviders.Clear(); if (saslHandler != null) { // Provider for "AMQP3100" SaslTransportProvider saslProvider = new SaslTransportProvider(AmqpVersion.V100); saslProvider.AddHandler(saslHandler); settings.TransportProviders.Add(saslProvider); } // Provider for "AMQP0100" AmqpTransportProvider amqpProvider = new AmqpTransportProvider(AmqpVersion.V100); settings.TransportProviders.Add(amqpProvider); AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings); TransportBase transport = await Task.Factory.FromAsync( (c, s) => initiator.BeginConnect(timeout, c, s), (r) => initiator.EndConnect(r), null).ConfigureAwait(false); try { AmqpConnectionSettings connectionSettings = new AmqpConnectionSettings() { ContainerId = Guid.NewGuid().ToString(), HostName = addressUri.Host }; AmqpConnection connection = new AmqpConnection(transport, settings, connectionSettings); await connection.OpenAsync(timeout).ConfigureAwait(false); return(connection); } catch { transport.Abort(); throw; } }
public async Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, SaslHandler saslHandler, TimeSpan timeout) { TransportSettings transportSettings; if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase)) { transportSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultPort }; } else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase)) { TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultSecurePort }; transportSettings = new TlsTransportSettings(tcpSettings) { TargetHost = addressUri.Host }; } #if NET45 else if (addressUri.Scheme.Equals(WebSocketTransport.WebSockets, StringComparison.OrdinalIgnoreCase) || addressUri.Scheme.Equals(WebSocketTransport.SecureWebSockets, StringComparison.OrdinalIgnoreCase)) { transportSettings = new WebSocketTransportSettings() { Uri = addressUri }; } #endif else { throw new NotSupportedException(addressUri.Scheme); } AmqpSettings settings = new AmqpSettings(); if (saslHandler != null) { // Provider for "AMQP3100" SaslTransportProvider saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(1, 0, 0)); saslProvider.AddHandler(saslHandler); settings.TransportProviders.Add(saslProvider); } // Provider for "AMQP0100" AmqpTransportProvider amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(new Version(1, 0, 0, 0))); settings.TransportProviders.Add(amqpProvider); AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings); TransportBase transport = await Task.Factory.FromAsync( (c, s) => initiator.BeginConnect(timeout, c, s), (r) => initiator.EndConnect(r), null); try { AmqpConnectionSettings connectionSettings = new AmqpConnectionSettings() { ContainerId = Guid.NewGuid().ToString(), HostName = addressUri.Host }; AmqpConnection connection = new AmqpConnection(transport, settings, connectionSettings); await connection.OpenAsync(timeout); return(connection); } catch { transport.Abort(); throw; } }