Executing background tasks in Asp.Net

I have a webapi2 service hosted in azure. I'm using a third-party library to send out SMS messages. This is a synchronous call and sometimes can take upwards of 10 seconds, blocking other requests from being handled during times of high traffic.

As far as I understand it, wrapping the method in Task. Run and then awaiting it, will NOT help, and may in fact worsen the situation. Task. Run will simply grab another thread from the same thread pool, but then incurs the overhead of having to switch back to the original context. From what I've read, Task. Run is really only useful for CPU-bound operations.

Without scaling my services, any ideas on how to increase throughput?

http://19216881.onl/
http://jiofi-local-html.in/
http://router-network.onl/
Last edited on
When the call is blocking i.e. doesn't participate in the async process it doesn't make sense to start an async task.

What you can do is starting an independent thread. See:

https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=net-5.0
https://www.tutorialspoint.com/asp.net/asp.net_multi_threading.htm

You need to take care of shared resources. And you need to wait for all started threads to end at least at the end of the program.
Registered users can post here. Sign in or register to post.