This is a method from my MVC controller:
public async Task RecorderInfo()
{
Stream stream = Response.OutputStream;
for (int i = 0; i < 100; i++)
{
byte[] data = new byte[100];
await stream.WriteAsync(data, 0, data.Length); // Send data to the client.
await Task.Delay(10000); // Wait for some event.
}
}
It is plainly that I want to give data back to the HTTP client whenever I want to, so, there are questions regarding this approach:
- Is it safe to hold the web response for so long? Aren't some thread pools going to run out of free threads when several requests will be made to this action?
- Are there any pitfalls I might come into?
- How would you do this, what would be your way to discretely notify the HTTP listener of some event or send him some data?
I think the more conventional approach is to poll the server using javascript. This approach is also used and called a long poll.
This document will be helpful if you are hosting in IIS. I would like to point out it recommends against long running tasks.
http://blogs.msdn.com/b/tmarq/archive/2010/04/14/performing-asynchronous-work-or-tasks-in-asp-net-applications.aspx
"In case you're curious, the IIS thread pool has a maximum thread count of 256. This thread pool is designed in such a way that it does not handle long running tasks well. "
If you have many requests your thread pool will fill up quickly.