Reputation: 3233
I created a WebAPI method that will insert data into a database from the contents posted to the method.
My question is how can I push processing off so that the requester isn't hanging around for a response?
For example:
[HttpPost]
[Route("MyTest/ImportData")]
public HttpResponseMessage AddData([FromBody] XElement xmlBody)
{
try
{
// Verify that I can read the XML..
// Push XML off to another process to insert into database.
UpdateDataBase(xmlBody);
// Notify requester that I accepted the file.
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
What I have works. However it may take up to 4 minutes to insert all of the data. I would like to verify that I can read the xml, push the process off somehow and give the user an OK response.
Upvotes: 2
Views: 115
Reputation: 6335
Delegate the work to some other process.
It could be whatever you want, a service, a command line app, whatever is appropriate.
I would setup a messaging system, have the webapi send all these work actions to a queue and then have a windows service for example process this queue. This will release the pressure on the api and let it handle other requests without waiting for long running tasks to complete. It doesn't have to be anything more complicated than this.
As a suggestion, RabbitMQ is fairly straight forward, but I would suggest you look around and pick whatever you like
Upvotes: 2