We have recently developed an ASP.Net WebAPI2 that is using the synchronous calls across various layers as below.
Now I have learnt that it's best to use the Async Task
approach for stability reasons but as the code is already developed for most of the functionality using sync calls, I was wondering what is the best way to convert the calls I have.
Say this is how my code is initially written:
[Route("user/{userId}/feeds ")]
[HttpGet]
public IEnumerable<NewsFeedItem> GetNewsFeedItemsForUserAsync(string userId)
{
return newsFeedService.GetNewsFeedItemsForUser(userId);
}
I can convert this to something like this:
[Route("user/{userId}/feeds ")]
[HttpGet]
public async Task<IEnumerable<NewsFeedItem>> GetNewsFeedItemsForUserAsync(string userId)
{
return await Task.Run(()=>newsFeedService.GetNewsFeedItemsForUser(userId));
}
But, if I understand correctly, I don't think that will help me scale up my website as it will still end up using the thread pool thread.
The only other option I see to modify all the layers and all the functions to use async
but that seems like a lot of work.
So I was wondering if anyone here had to undergo a similar exercise and what is the best way to address the problem.
Regards Kiran