async void CloseServiceConnection(IChannelHandlerContext context, Exception cause, PublishPacket will) { if (!this.ConnectedToService) { // closure happened before IoT Hub connection was established or it was initiated due to disconnect return; } try { foreach (var publishProcessor in this.publishProcessors) { publishProcessor.Value.Complete(); } this.publishPubAckProcessor.Complete(); this.publishPubRecProcessor.Complete(); this.pubRelPubCompProcessor.Complete(); await Task.WhenAll( this.CompletePublishAsync(context, will), this.publishPubAckProcessor.Completion, this.publishPubRecProcessor.Completion, this.pubRelPubCompProcessor.Completion); IMessagingBridge bridge = this.messagingBridge; this.messagingBridge = null; await bridge.DisposeAsync(cause); } catch (Exception ex) { CommonEventSource.Log.Info("Failed to close IoT Hub Client cleanly.", ex.ToString(), this.ChannelId); } }
async void ShutdownOnReceiveError(Exception cause) { this.publishPubAckProcessor.Close(); foreach (var publishProcessor in this.publishProcessors) { publishProcessor.Value.Close(); } this.publishPubRecProcessor.Close(); this.pubRelPubCompProcessor.Close(); IMessagingBridge bridge = this.messagingBridge; if (bridge != null) { this.messagingBridge = null; try { await bridge.DisposeAsync(cause); } catch (Exception ex) { CommonEventSource.Log.Info("Failed to close IoT Hub Client cleanly: " + ex, this.ChannelId, this.Id); } } ShutdownOnError(this.capturedContext, ReceiveProcessingScope, cause); }
async Task CloseServiceConnection(IChannelHandlerContext context, Exception cause, PublishPacket will) { if (!this.ConnectedToService) { // closure happened before IoT Hub connection was established or it was initiated due to disconnect if (cause != null) { string causeScope = (string)cause.Data[OperationScopeExceptionDataKey] ?? "unknown"; CommonEventSource.Log.Error($"Connection closed while not fully connected. Scope: {causeScope}", cause, this.ChannelId, this.Id); } return; } try { this.publishPubAckProcessor.Close(); this.publishPubRecProcessor.Close(); this.pubRelPubCompProcessor.Close(); await Task.WhenAll( this.CompletePublishAsync(context, will), this.publishPubAckProcessor.Closed, this.publishPubRecProcessor.Closed, this.pubRelPubCompProcessor.Closed); } catch (Exception ex) { CommonEventSource.Log.Info("Failed to complete the processors: " + ex.ToString(), this.ChannelId, this.Id); } try { IMessagingBridge bridge = this.messagingBridge; if (this.messagingBridge != null) { this.messagingBridge = null; await bridge.DisposeAsync(cause); } } catch (Exception ex) { CommonEventSource.Log.Info("Failed to close IoT Hub Client cleanly: " + ex.ToString(), this.ChannelId, this.Id); } }