Exemplo n.º 1
0
        public async Task RunAsync(
            [ServiceBusTrigger("%MARKET_DATA_QUEUE_TOPIC_NAME%", Connection = "MARKET_DATA_QUEUE_CONNECTION_STRING")] QueueMessage queueMessage,
            ILogger logger)
        {
            if (queueMessage == null || queueMessage.MessageType == null || queueMessage.Message == null)
            {
                throw new ArgumentNullException(nameof(queueMessage));
            }

            logger.LogInformation($"C# ServiceBus queue trigger function processed message: {queueMessage}");
            logger.LogInformation($"With type: {queueMessage.MessageType}");
            logger.LogInformation($"With value: {queueMessage.Message}");

            var messageType = _resolver.GetTypeByCategory(queueMessage.MessageType);

            if (messageType == null)
            {
                throw new ArgumentException($"Invalid category: {queueMessage.MessageType}");
            }

            var hubRequest = await _rehydrator.RehydrateAsync(queueMessage.Message, messageType).ConfigureAwait(false);

            if (hubRequest == null)
            {
                throw new ArgumentException(nameof(queueMessage.Message));
            }

            await _mediator.Send(hubRequest).ConfigureAwait(false);

            await Task.CompletedTask.ConfigureAwait(false);
        }
        internal static async Task <IHubMessage?> RehydrateAsync(this IHubRehydrator rehydrator, string input, Type messageType)
        {
            if (rehydrator == null)
            {
                throw new ArgumentNullException(nameof(rehydrator));
            }

            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }

            await using (var stream = new MemoryStream())
                await using (var writer = new StreamWriter(stream))
                {
                    await writer.WriteAsync(input).ConfigureAwait(false);

                    await writer.FlushAsync().ConfigureAwait(false);

                    stream.Position = 0;
                    return(await rehydrator.RehydrateAsync(stream, messageType).ConfigureAwait(false));
                }
        }
Exemplo n.º 3
0
        public async Task RunAsync(
            [EventHubTrigger("%REQUEST_QUEUE_NAME%", Connection = "REQUEST_QUEUE_CONNECTION_STRING")] MessageEnvelope eventData,
            ILogger logger)
        {
            if (eventData is null)
            {
                throw new ArgumentNullException(nameof(eventData));
            }

            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            logger.LogInformation($"{FunctionName} started processing a request.");

            var requestType = _resolver.GetTypeByCategory(eventData.MessageType);

            if (requestType == null)
            {
                logger.LogWarning("{messageType} not found", eventData.MessageType);
                return; // TODO: go to error queue or storage?
            }

            using (var body = new MemoryStream(Encoding.UTF8.GetBytes(eventData.Message)))
            {
                var hubRequest = await _rehydrator.RehydrateAsync(body, requestType).ConfigureAwait(false);

                if (hubRequest == null)
                {
                    logger.LogWarning("CommandRouter: could not rehydrate {messageType}", eventData.MessageType);
                    return; // TODO: go to error queue or storage?
                }

                await _commandDispatcher.DispatchAsync(hubRequest).ConfigureAwait(false);
            }
        }