locked
How to send response to the caller before a task is completed RRS feed

  • Question

  • User-762416054 posted

    I am working on a project that integrates two applications via REST/JOSN API. In general, App A will send a request to Application B. App B will process the request and send the result back. If for a task that will take more than a few milliseconds for App B to process, then App B is required to send a response immediately to App A acknowledging the request is received. And it then will send the result back to App A via a POST API call to App A once the task is completed.

    I am wondering how this can be done in App B. I tried using Event-Based Asynchronous Patten. It does not work as it throws an exception of "An asynchronous module or handler completed while an asynchronous operation was still pending."

    Any suggestions or help will be greatly appreciated.

    jwang

    Tuesday, October 16, 2018 6:41 PM

Answers

All replies

  • User475983607 posted

    Assuming that App B is a web application, fire up a background task to finish whatever processing is required and return a response to the caller; App A.  Invoke the POST to App A once the process on App B competes.

    Tuesday, October 16, 2018 7:14 PM
  • User-762416054 posted

    mgebhard,

    Thanks for your response. Would you please elaborate how to fire up a background task in the ApiController-based controller?  Any sample code that you can point me to? As I mentioned, I tried to fire off a separate thread using Event-Based Asynchronous Patten, but it appears that the controller needs to wait for the completion of asynchronous process before it can return a response.

    jwang

    Wednesday, October 17, 2018 4:40 AM
  • User-762416054 posted

    I found a new API in .NET Framework 4.5.2 or higher, that is, the HostingEnvironment.QueueBackgroundWorkItem HostingEnvironment.QueueBackgroundWorkItem HostingEnvironment.QueueBackgroundWorkItem Method, which apparently provides the functionality I am looking for.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.hosting.hostingenvironment.queuebackgroundworkitem?view=netframework-4.7.2

    Wednesday, October 17, 2018 9:51 PM
  • User1724605321 posted

    Hi jwang_ga ,

    QueueBackgroundWorkItem is added from .NET 4.5.2 , and you can also find other solutions which provide background task feature such as QUARTZ.NET , FluentScheduler :

    https://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx 

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2018 1:49 AM