asp-net-web-api[1]

Call Asynchronous Methods and return Results within ASP Web API

So the other day my boss asked me to implement a few Azure Push Notifications related Web methods in one of our ASP Web API projects I had developed. Those Web methods were suppose to send Push Notification messages to the Azure Notification Hub, where as those notifications will be delivered to the User’s mobile devices.

When I started implementing I realized when calling the Azure Notification Hub using the Windows Azure Library, I realized there’s only Asynchronous calls available to communicate with the Notification Hub. It got me thinking, How on earth am I gonna call Asynchronous methods inside the ASP Web API (as I haven’t tried that before). Well, after a little bit of playing around I figured it out thanks to some documentations in ASP.NET website.

So here I am sharing the code I used for you all, if you ever come across such an implementation, how to call an Asynchronous methods inside the ASP Web API.

namespace YourNameSpace.Controllers
{
    [RoutePrefix("api/AsyncTest")]
    public class AsyncTestController : ApiController
    {
        [Route("AsyncWebMethod/{whateverValue}", Name = "DoSomeAsyncProcessingAndReturnResult")]
        [HttpPost]
        public async Task<HttpResponseMessage> DoSomeAsyncProcessingAndReturnResult(string whateverValue)
        {
            string Result = await WhateverAsyncMethodCallYouWantToCallInsideWebAPI(whateverValue);

            return Request.CreateResponse(HttpStatusCode.OK, Result);
        }
    }
}

 

Have fun Coding fellas ! 😀

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s