2

I have a web api (written in c# on ASP.NET Web API 2). One of the methods takes a HTTP POST and adds it to an Azure Queue.

I'd like to know how I can get hold of a http status code (or whatever is available) from the Azure Queue so that I can return an appropriate http status code from my method.

The code so far is similar to this:

[Route("api/v1/somewhere/")]
        [ResponseType(typeof(Thingy))]
        public async Task<IHttpActionResult> Post(Thingy thingy)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString());
            var queueClient = storageAccount.CreateCloudQueueClient();
            var queue = queueClient.GetQueueReference("thingys");

            var msg = new CloudQueueMessage(JsonConvert.SerializeObject(thingy));
            await queue.AddMessageAsync(msg);

            return Json(thingy);
        }

I'd like to swap out the return Json(thingy); line and return a http status code 202 if all goes well with the await queue.AddMessageAsync(msg); and another appropriate code if something goes wrong.

Any ideas?

1
  • I've added an answer below, please let me know if it solved your problem Commented Aug 12, 2015 at 13:41

2 Answers 2

2

You should make use of LastResult property of OperationContext object. It has a HttpStatusCode property which will return the status code from Storage Service. Here's an example of the same:

    static void QueueTest1()
    {
        var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        var queueClient = account.CreateCloudQueueClient();
        var queue = queueClient.GetQueueReference("my-queue");
        queue.CreateIfNotExists();
        var messageContents = "This is a test message";
        var opContext = new OperationContext();
        var msg = new CloudQueueMessage(messageContents);
        queue.AddMessage(msg, null, null, null, opContext);
        var statusCode = opContext.LastResult.HttpStatusCode;
        Console.WriteLine("Status Code = " + statusCode);//Prints 201 as status code
        queue.DeleteIfExists();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Check out this page: http://bitoftech.net/2014/03/05/new-asp-net-web-api-2-ihttpactionresult-cors-tutorial/

I think you should wrapp the response you want in your own class, something like that:

public class MyResponse : IHttpActionResult
{
    public string Message { get; private set; }
    public HttpRequestMessage Request { get; private set; }


    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(ExecuteResult());
    }

    public HttpResponseMessage ExecuteResult()
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Accepted);

        response.Content = new StringContent(Message);
        response.RequestMessage = Request;
        return response;
    }
}

3 Comments

I'm happy with returning a straight HttpStatusCode.Accepted, I just wanted to check that an entity had been made in the Azure Queue so was looking at how to get a HTTP status code from the Azure Queue. Then I could conditionally return a code 202 so that API clients using my code knew that the request had been primarily accepted but not fully processed. But I can see that this would have been useful if I wanted to return a message along with the status code :)
I see, but is it browser on the other side or another application? Since if you return 202 code, the browser won't send another request (unless you handled it with JS, ajax, etc etc) to get a response when the original request has been processed. Is there something I'm missing?
not looking just to support web. I want a 202 as the other side of the queue doesn't process immediately (a web job on Azure). So I don't want the client hanging around for another response :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.