ASYNCHRONOUS CALLBACK FROM WEB API CONTROLLER

john rob

Total Post:108

Points:756
Posted by  john rob
C# 
C#
 1624  View(s)
Ratings:
Rate this:

I'm very new to Web API and I have an unusual pattern that I need to implement. In the Post method of my controller, it is to take an object which includes a CallbackURL. It will then immediately return an HTTP response to the caller. Afterwards, it will use a 3rd party, off-site API to perform some work with the object. Once that work is done, the controller is to post the results of that work to the CallbackURL.

However, I do not know how to implement this in Web API. Once I return the HTTP response, the controller's lifecycle is over, correct? If so, how do I perform the work I need to do after I return the response?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Asynchronous callback from web api controller

    Hi John,

    If you only need to post results to a url and not to the client that initiated the call, you could possibly do something as easy as this:

    public string MyAPIMethod(object input)

    {

        Task.Factory.StartNew(() =>

        {       

            //call third-party service and post result to callback url here.       

        });

        return "Success!";

    }

    The api call will return right away, and the Task you created will continue the processing in a different thread.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!