I have a button that makes a REST request to create a document:
private void Click(object sender, ActionBaseEventArgs e)
{
RequestQuotationDocument(quotation.Oid).ConfigureAwait(false);
}
private async System.Threading.Tasks.Task RequestQuotationDocument(int quotationId)
{
HttpClient client = new HttpClient();
var values = new
{
DocumentType = "Quotation",
Id = quotationId.ToString()
};
var content = new StringContent(JsonConvert.SerializeObject(values), System.Text.Encoding.UTF8,
"application/json");
await client.PostAsync(url, content);
}
However the page is still waiting for the response of the POST request. How do I make this truly async?
CodePudding user response:
How do I make this truly async?
In ASP.NET, await
yields to the thread pool, not the client.
If possible, I recommend changing the client so that a longer request is handled appropriately; e.g., calling this via JavaScript instead of a click handler, and updating the page when it completes.
If you really need to return early from an web request, then you should implement a basic distributed architecture. As described on my blog, this consists of two parts:
- A durable queue of work.
- A backend processor of that queue.