Example #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;
            }
        }
Example #2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            ProxiedHttpMethod method;

            switch (request.Method.Method)
            {
            case "POST":
                method = ProxiedHttpMethod.Post;
                break;

            case "PUT":
                method = ProxiedHttpMethod.Put;
                break;

            case "PATCH":
                method = ProxiedHttpMethod.Patch;
                break;

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

            ProxiedHttpRequestModel proxiedRequestModel = new ProxiedHttpRequestModel(method, await request.Content.ReadAsStringAsync(), $"{AuthTokenProvider.RetrieveType()} {AuthTokenProvider.Retrieve()}", request.RequestUri.AbsolutePath);

            try
            {
                await AzureServiceBusClient.SendAsync(new Message(UTF8Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(proxiedRequestModel))));
            }
            catch (Exception e)
            {
                if (Logger.IsErrorEnabled)
                {
                    Logger.Error($"Failed to send proxied Azure Service Bus HTTP Message. Reason: {e.ToString()}");
                }

                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent($"Client Exception: {e.ToString()}")
                });
            }

            //Indicate accepted since it's not technically handled.
            return(new HttpResponseMessage(HttpStatusCode.Accepted));
        }