public async Task EnableMethodsAsync(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, timeout, $"{nameof(EnableMethodsAsync)}"); } try { Debug.Assert(_methodSendingLink == null); Debug.Assert(_methodReceivingLink == null); string correlationIdSuffix = Guid.NewGuid().ToString(); Task <AmqpIoTReceivingLink> receiveLinkCreator = _amqpIoTSession.OpenMethodsReceiverLinkAsync(_deviceIdentity, correlationIdSuffix, timeout); Task <AmqpIoTSendingLink> sendingLinkCreator = _amqpIoTSession.OpenMethodsSenderLinkAsync(_deviceIdentity, correlationIdSuffix, timeout); await Task.WhenAll(receiveLinkCreator, sendingLinkCreator).ConfigureAwait(false); _methodReceivingLink = receiveLinkCreator.Result; _methodSendingLink = sendingLinkCreator.Result; _methodReceivingLink.RegisterMethodListener(OnMethodReceived); _methodSendingLink.Closed += OnLinkDisconnected; _methodReceivingLink.Closed += OnLinkDisconnected; if (Logging.IsEnabled) { Logging.Associate(this, _methodReceivingLink, $"{nameof(_methodReceivingLink)}"); } if (Logging.IsEnabled) { Logging.Associate(this, _methodSendingLink, $"{nameof(_methodSendingLink)}"); } } catch (Exception) { _methodReceivingLink?.Abort(); _methodReceivingLink = null; _methodSendingLink?.Abort(); _methodSendingLink = null; throw; } finally { if (Logging.IsEnabled) { Logging.Exit(this, timeout, $"{nameof(EnableMethodsAsync)}"); } } }
public async Task EnsureTwinLinksAreOpenedAsync(TimeSpan timeout) { if (Volatile.Read(ref _twinLinksOpened) == true) { return; } if (Logging.IsEnabled) { Logging.Enter(this, timeout, $"{nameof(EnsureTwinLinksAreOpenedAsync)}"); } try { await _twinLinksLock.WaitAsync().ConfigureAwait(false); if (_twinLinksOpened) { return; } Debug.Assert(_twinSendingLink == null); Debug.Assert(_twinReceivingLink == null); string correlationIdSuffix = Guid.NewGuid().ToString(); Task <AmqpIoTReceivingLink> receiveLinkCreator = _amqpIoTSession.OpenTwinReceiverLinkAsync(_deviceIdentity, correlationIdSuffix, timeout); Task <AmqpIoTSendingLink> sendingLinkCreator = _amqpIoTSession.OpenTwinSenderLinkAsync(_deviceIdentity, correlationIdSuffix, timeout); await Task.WhenAll(receiveLinkCreator, sendingLinkCreator).ConfigureAwait(false); _twinSendingLink = sendingLinkCreator.Result; _twinSendingLink.Closed += OnLinkDisconnected; _twinReceivingLink = receiveLinkCreator.Result; _twinReceivingLink.RegisterTwinListener(OnDesiredPropertyReceived); _twinReceivingLink.Closed += OnLinkDisconnected; _twinLinksOpened = true; if (Logging.IsEnabled) { Logging.Associate(this, this, _twinReceivingLink, $"{nameof(EnsureTwinLinksAreOpenedAsync)}"); } if (Logging.IsEnabled) { Logging.Associate(this, this, _twinSendingLink, $"{nameof(EnsureTwinLinksAreOpenedAsync)}"); } } catch (Exception ex) when(!ex.IsFatal()) { _twinReceivingLink?.Abort(); _twinSendingLink?.Abort(); _twinReceivingLink = null; _twinSendingLink = null; throw; } finally { _twinLinksLock.Release(); if (Logging.IsEnabled) { Logging.Exit(this, timeout, $"{nameof(EnsureTwinLinksAreOpenedAsync)}"); } } }