First return status code but how ?

APIPointNewbie 146 Reputation points
2021-11-15T15:50:51.823+00:00

Hello Community,

I have a question about the return of StatusCodes in ASP Net Core (C#).

I have a very large process that is started with a call to an API endpoint, this process is running Async, but it still takes a very long time to return the status code 202 Accepted, it takes around 3 to 5 seconds.

I am looking for a way to return the status code first and then start processing.
Exemplary structure:

[HttpPost]
public async Task<ActionResult<string>> Post()
{
await StartProcess();
return Accepted();
}
    }

private async Task StartProcess()
{
//.........
}

I would be very grateful for any help.

Greetings

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,305 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,586 Reputation points
    2021-11-15T16:29:47.547+00:00

    it depends on if the caller needs to know success. if not, just run the process on background thread and don't await, just return. If you need status, then it is more complex. you will need the process write its status to a database or common memory. then the client can poll or long poll for the status.

    you could also use signal/r a use its messaging.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.