private async Task MessageHandler(Message message, CancellationToken cancellationToken)
        {
            if (await _subscriptionClient.WaitForPause(message, cancellationToken))
            {
                return;
            }

            // Note: About exception handling
            // Any exception leaving this method will cause the message to be retried later.
            // If you consider the exception to be permanent, complete the message.
            // This is just examples of error handling
            AvailableBlobEvent metadata;

            try
            {
                metadata = message.DeserializeJsonBody <AvailableBlobEvent>();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Permanent error: {ex.Message}");
                await _subscriptionClient.CompleteMessage(message);

                return;
            }

            Console.WriteLine(
                $"Got message of type '{metadata.BlobType}' and with correlation-id '{metadata.CorrelationId}', uri '{metadata.Uri}'. Content had format '{metadata.ContentType}'");

            // Save incoming blob to a temporary file. This could of course be done in memory, or to some database
            var blobLocalStorage = LocalStorageUtility.GetNewWorkingFile();
            await _blobDownloader.DownloadBlobAsync(metadata, blobLocalStorage, cancellationToken);

            await ProcessFile(message.MessageId, metadata, blobLocalStorage);

            await _subscriptionClient.CompleteMessage(message);

            LocalStorageUtility.RemoveWorkingFile(blobLocalStorage);
        }
Example #2
0
        private async Task MessageHandler(Message message, CancellationToken cancellationToken)
        {
            if (await _subscriptionClient.WaitForPause(message, cancellationToken))
            {
                return;
            }

            // Note: If anything in this method throws an exception, the message-handling will be retried later.
            // To prevent this, catch the exception here, figure out if it is transient or permanent and complete the message depending on that.
            // This is just examples of error handling
            AvailableBlobEvent metadata;

            try
            {
                metadata = message.DeserializeJsonBody <AvailableBlobEvent>();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Permanent error: {ex.Message}");
                await _subscriptionClient.CompleteMessage(message);

                return;
            }

            LogMessage(metadata);

            var blobLocalStorage = LocalStorageUtility.GetNewWorkingFile();

            await _blobDownloader.DownloadBlobAsync(metadata, blobLocalStorage);

            await ProcessFile(message.MessageId, metadata, blobLocalStorage);

            await _subscriptionClient.CompleteMessage(message);

            LocalStorageUtility.RemoveWorkingFile(blobLocalStorage);
        }