コード例 #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger Test Storing a message!");

            var message = new ArchivedMessage(new TestMessage()
            {
                Description = "This is a test message with a random value of " + new Random().Next(0, int.MaxValue)
            });

            ICosmosDbContainer cosmosContainer = await _cosmosClient.GetContainer <ArchivedMessage>();

            CosmosDbResponse <ArchivedMessage> cosmosResponse = await cosmosContainer.Add(message.Id, message);

            return(new OkObjectResult($"Cosmos Response: {cosmosResponse.IsSuccessful}"));
        }
コード例 #2
0
        public async Task Run([ServiceBusTrigger("%ServiceBusTopic%", "%ServiceBusSubscription%", Connection = "ServiceBusConnectionString")] string messageString, ILogger log)
        {
            MessageBase originalMessage = null;

            try
            {
                originalMessage = _messageReader.ReadMessage(messageString);
                var archivedMessage = new ArchivedMessage(originalMessage);

                if (archivedMessage.MessageTypeName.Equals(nameof(HealthCheckRequest)))
                {
                    await HandleHealthCheckRequest(_messageReader.ReadMessage <HealthCheckRequest>(messageString));
                }

                ICosmosDbContainer cosmosContainer = await _cosmosClient.GetContainer <ArchivedMessage>();

                CosmosDbResponse <ArchivedMessage> cosmosResponse =
                    await cosmosContainer.Add(archivedMessage.Id, archivedMessage);

                if (!cosmosResponse.IsSuccessful)
                {
                    throw new ApplicationException(
                              $"MessageStorage failed to write message {archivedMessage.Message.MessageId} of type {archivedMessage.Message.MessageType}. {cosmosResponse.ErrorMessage}");
                }
            }
            catch (Exception storeException)
            {
                try
                {
                    log.LogError(storeException, "exception saving to MessageStorage. OriginalMessage: " + messageString);
                    await _messageSender.SendException(storeException, originalMessage);
                }
                catch (Exception sendException)
                {
                    log.LogCritical(sendException, "Exception attempting to save an error message to the bus! This is very bad!");
                }
                throw;
            }
        }