public async Task ProcessAsync <T>(IMessageStateHandler <T> messageStateHandler, IPipelineInformation pipelineInformation, IMessageProcessor next, CancellationToken cancellationToken) where T : class, IMessage { IMessageAttachment attachment = null; var queueName = AutoMessageMapper.GetQueueName <T>(); try { string attachmentId = null; if (typeof(ICommandWithAttachment).IsAssignableFrom(typeof(T))) { attachmentId = AttachmentUtility.GetAttachmentIds(messageStateHandler.MessageProperties).FirstOrDefault(); if (!string.IsNullOrEmpty(attachmentId)) { attachment = await _attachmentProvider.GetAttachmentAsync(queueName, attachmentId, cancellationToken).ConfigureAwait(false); var message = (ICommandWithAttachment)await messageStateHandler.GetMessageAsync().ConfigureAwait(false); message.Attachment = attachment; } } await next.ProcessAsync(messageStateHandler, cancellationToken).ConfigureAwait(false); if (attachment != null) { attachment.Stream?.Dispose(); await _attachmentProvider.DeleteAttachmentAsync(queueName, attachmentId, cancellationToken).ConfigureAwait(false); } } finally { attachment?.Stream?.Dispose(); } }