Reputation: 33071
I have inherited some code which was written in web api that I am not sure I understand 100%. It looks similar to this:
public async Task<IHttpActionResult> GetAll()
{
var items = await repo.GetItems();
Task.Run(async () =>
{
await MakeCallToExternalServiceAsync();
await MakeCallToAnotherExternalServiceAsync();
});
return Ok(items);
}
After the call to the repository to get some data from the data store, the code fires off a task to make some calls to some external services. Assuming none of these calls fail are there any other issues with this? Since the request is finished are these calls going to be aborted? There might be resources that are "request scope" using Autofac - would those get disposed since the request has finished?
Upvotes: 1
Views: 1039
Reputation: 456342
This is the "poor man's" way of returning early from an ASP.NET request. A.K.A. "fire and forget". There are much better solutions available.
Assuming none of these calls fail are there any other issues with this?
Well, if any of them do fail, you would never know.
Since the request is finished are these calls going to be aborted?
Not necessarily. They may run to completion, or they may be aborted. ASP.NET will not abort them just because the request finishes, but IIS will abort them whenever it decides to recycle the AppDomain / process (which it does do occasionally).
There might be resources that are "request scope" using Autofac - would those get disposed since the request has finished?
Yes. Once the request has finished, using any request-scoped resources is not a good idea.
Upvotes: 3