Esempio n. 1
0
        protected override async Task HandleQueueIncomingMessageAsync(Message message, CancellationToken cancellationToken)
        {
            try
            {
                //TODO: Handle failure case.
                //Assume all messages in this queue are proxied request models.
                ProxiedHttpRequestModel httpRequestModel = JsonConvert.DeserializeObject <ProxiedHttpRequestModel>(Encoding.UTF8.GetString(message.Body));

                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Sending proxied Azure Service Queue HTTP Request. Method: {httpRequestModel.Method.ToString()} Route: {httpRequestModel.Route} Content: {httpRequestModel.SerializedJsonBody}");
                }

                //Skip the first slash.
                string route = httpRequestModel.Route.Remove(0, 1);

                switch (httpRequestModel.Method)
                {
                case ProxiedHttpMethod.Post:
                    await ProxyClient.SendProxiedPostAsync(route, httpRequestModel.SerializedJsonBody, httpRequestModel.AuthorizationToken);

                    break;

                case ProxiedHttpMethod.Put:
                    await ProxyClient.SendProxiedPutAsync(route, httpRequestModel.SerializedJsonBody, httpRequestModel.AuthorizationToken);

                    break;

                case ProxiedHttpMethod.Patch:
                    await ProxyClient.SendProxiedPatchAsync(route, httpRequestModel.SerializedJsonBody, httpRequestModel.AuthorizationToken);

                    break;

                default:
                    throw new ArgumentOutOfRangeException($"Cannot handle MethodType: {httpRequestModel.Method}");
                }

                //Indicate that the message has been consumed if we haven't canceled.
                if (!cancellationToken.IsCancellationRequested)
                {
                    await ServiceQueue.CompleteAsync(message.SystemProperties.LockToken);
                }
            }
            catch (Exception e)
            {
                //If not canceled, we want to abandon it so that other consumers maybe can handle it.
                if (!cancellationToken.IsCancellationRequested)
                {
                    await ServiceQueue.AbandonAsync(message.SystemProperties.LockToken);
                }

                throw;
            }
        }