public async Task <AmqpIotConnection> OpenConnectionAsync(CancellationToken cancellationToken) { if (Logging.IsEnabled) { Logging.Enter(this, nameof(OpenConnectionAsync)); } var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(s_amqpVersion_1_0_0); var amqpSettings = new AmqpSettings(); amqpSettings.TransportProviders.Add(amqpTransportProvider); var amqpConnectionSettings = new AmqpConnectionSettings { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = CommonResources.GetNewStringGuid(), HostName = _hostName, IdleTimeOut = Convert.ToUInt32(_amqpTransportSettings.IdleTimeout.TotalMilliseconds), }; _amqpIotTransport = new AmqpIotTransport(amqpSettings, _amqpTransportSettings, _hostName, s_disableServerCertificateValidation); TransportBase transportBase = await _amqpIotTransport.InitializeAsync(cancellationToken).ConfigureAwait(false); try { var amqpConnection = new AmqpConnection(transportBase, amqpSettings, amqpConnectionSettings); var amqpIotConnection = new AmqpIotConnection(amqpConnection); amqpConnection.Closed += amqpIotConnection.AmqpConnectionClosed; await amqpConnection.OpenAsync(cancellationToken).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(OpenConnectionAsync)}"); } return(amqpIotConnection); } catch (Exception ex) when(!ex.IsFatal()) { transportBase?.Close(); _amqpIotTransport?.Dispose(); throw; } finally { if (Logging.IsEnabled) { Logging.Exit(this, nameof(OpenConnectionAsync)); } } }
public async Task <AmqpIotSession> OpenSessionAsync(IDeviceIdentity deviceIdentity, CancellationToken cancellationToken) { if (Logging.IsEnabled) { Logging.Enter(this, deviceIdentity, nameof(OpenSessionAsync)); } AmqpIotConnection amqpIotConnection = await EnsureConnectionAsync(cancellationToken).ConfigureAwait(false); AmqpIotSession amqpIotSession = await amqpIotConnection.OpenSessionAsync(cancellationToken).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Associate(amqpIotConnection, amqpIotSession, nameof(OpenSessionAsync)); Logging.Exit(this, deviceIdentity, nameof(OpenSessionAsync)); } return(amqpIotSession); }
public async Task <IAmqpAuthenticationRefresher> CreateRefresherAsync(DeviceIdentity deviceIdentity, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, deviceIdentity, timeout, nameof(CreateRefresherAsync)); } AmqpIotConnection amqpIotConnection = await EnsureConnectionAsync(timeout).ConfigureAwait(false); IAmqpAuthenticationRefresher amqpAuthenticator = await amqpIotConnection .CreateRefresherAsync(deviceIdentity, timeout) .ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(this, deviceIdentity, timeout, nameof(CreateRefresherAsync)); } return(amqpAuthenticator); }
public async Task <IAmqpAuthenticationRefresher> CreateRefresherAsync(IDeviceIdentity deviceIdentity, CancellationToken cancellationToken) { if (Logging.IsEnabled) { Logging.Enter(this, deviceIdentity, nameof(CreateRefresherAsync)); } AmqpIotConnection amqpIotConnection = await EnsureConnectionAsync(cancellationToken).ConfigureAwait(false); IAmqpAuthenticationRefresher amqpAuthenticator = await amqpIotConnection .CreateRefresherAsync(deviceIdentity, cancellationToken) .ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(this, deviceIdentity, nameof(CreateRefresherAsync)); } return(amqpAuthenticator); }
public async Task <AmqpIotSession> OpenSessionAsync(DeviceIdentity deviceIdentity, TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, deviceIdentity, timeout, nameof(OpenSessionAsync)); } AmqpIotConnection amqpIotConnection = await EnsureConnectionAsync(timeout).ConfigureAwait(false); AmqpIotSession amqpIotSession = await amqpIotConnection.OpenSessionAsync(timeout).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Associate(amqpIotConnection, amqpIotSession, nameof(OpenSessionAsync)); } if (Logging.IsEnabled) { Logging.Exit(this, deviceIdentity, timeout, nameof(OpenSessionAsync)); } return(amqpIotSession); }
public async Task <AmqpIotConnection> EnsureConnectionAsync(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, timeout, nameof(EnsureConnectionAsync)); } AmqpIotConnection amqpIotConnection = null; IAmqpAuthenticationRefresher amqpAuthenticationRefresher = null; bool gain = await _lock.WaitAsync(timeout).ConfigureAwait(false); if (!gain) { throw new TimeoutException(); } try { if (_amqpIotConnection == null || _amqpIotConnection.IsClosing()) { if (Logging.IsEnabled) { Logging.Info(this, "Creating new AmqpConnection", nameof(EnsureConnectionAsync)); } // Create AmqpConnection amqpIotConnection = await _amqpIotConnector.OpenConnectionAsync(timeout).ConfigureAwait(false); if (_deviceIdentity.AuthenticationModel != AuthenticationModel.X509) { if (_deviceIdentity.AuthenticationModel == AuthenticationModel.SasGrouped) { if (Logging.IsEnabled) { Logging.Info(this, "Creating connection width AmqpAuthenticationRefresher", nameof(EnsureConnectionAsync)); } amqpAuthenticationRefresher = new AmqpAuthenticationRefresher(_deviceIdentity, amqpIotConnection.GetCbsLink()); await amqpAuthenticationRefresher.InitLoopAsync(timeout).ConfigureAwait(false); } } _amqpIotConnection = amqpIotConnection; _amqpAuthenticationRefresher = amqpAuthenticationRefresher; _amqpIotConnection.Closed += OnConnectionClosed; if (Logging.IsEnabled) { Logging.Associate(this, _amqpIotConnection, nameof(_amqpIotConnection)); } } else { amqpIotConnection = _amqpIotConnection; } } catch (Exception ex) when(!ex.IsFatal()) { amqpAuthenticationRefresher?.StopLoop(); amqpIotConnection?.SafeClose(); throw; } finally { _lock.Release(); } if (Logging.IsEnabled) { Logging.Exit(this, timeout, nameof(EnsureConnectionAsync)); } return(amqpIotConnection); }