0

I'm really new to ASP.NET Web API and just web development in general.

I have a POST method that will be adding a new item to my database.

[HttpPost]
public IHttpActionResult Post([FromBody] Item item)
{
    try
    {
        // ADD ITEM TO DATABASE
    }
    catch
    {
        return BadRequest("ERROR");
    }

}

My issue is that before adding the item to the database there is a lot of processing that happens(such as calling another, external, API, etc) which takes some time. I don't want the API to be held up if another request comes in while the current request is being processed.

What I'd like to do is add the item to be processed to some queue and have some separate thread running in the background pulling items out of the queue and processing them.

Any suggestions on how to handle this?

1
  • You can use async and await for I don't want the API to be held up if another request comes in while the current request is being processed Dec 15, 2016 at 8:06

1 Answer 1

3

You can easily create a thread when application start to handle the background process. Just create a Queue and push your item to it. However, I recommend to use Hangfire, it's very convenient to use. You just need to create a Hangfire job for each api call. Hangfire will execute them for you. http://hangfire.io/

var jobId = BackgroundJob.Enqueue(
    () => Console.WriteLine("Fire-and-forget!"));

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.