I have an ASP Core app that basically acts as a proxy
public IActionResult PostFileRequest(IFormFile file)
{
var httpRequest = new HttpRequestMessage()
{
Content = new StreamContent(file.OpenReadStream())
};
var task = httpClient.SendAsync(httpRequest);
// we store the task internally to be later processed by background services
// we don't want the user to wait for the response
return Ok();
}
The problem with this logic is that the user can get the response even before the httpRequest
is sent. The file stream gets disposed, and I get the exception. I do not want the client to wait for the response. Is there some separate Task
that completes when HttpClient
finishes sending the request? After the file is sent I don't need the stream anymore, and can safely answer the user and close the context.
UPD: in case the comment in the code was not clear enough: no, I do not ignore the result of SendAsync
. I process it in the background service. But the response to the user will always be the same regardless of the result, so the task here is to answer to the user and release the context as soon as possible.