2

My application load asynchronously a big amount of information from a web service and "Application_Start".

If an user request wants to use that information, and it is nor ready, the thread will be block with a Monitor.Wait. When the information is ready, the cache object will Monitor.Pulse all waiting threads. This should be fine, since the information takes a dozen of seconds, and the user has to be redirected to the login page, post login information, and be redirected again.

The problem, is that Monitor.Wait will block the CLR ThreadPool thread, and as far as I know if a burst of requests arrives asking for the "big information", the application could remain block by CLR ThreadPool starvation (I have a little mess with the current IIS/ASP.NET thread gating).

Since the big piece of information comes from a web service that I call asynchronously, I have the IAsyncResult of that operation.

So, Is there a way to tell a CLR ThreadPool Thread "Wait for this IOCP", so the threadpool thread can start to attend other call?

I have the feeling that this is not well explained, let me know if it is not clear what I am asking.

Regards.

PS: Although the bounty is over, if anybody knows a way to do that I will raise a new one and grant the author.

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • 2
    +1 for a good and clearly explained question. Do all pages of your application require this information or only some of them? Does the first request always lands to a particular page or could it be any page? – Darin Dimitrov Nov 28 '11 at 17:20
  • Thanks. Only some of the pages required this information. Considering a shutdown application domain, and that the first request force IIS to start it, the first request will be unauthenticated (since we keep some small login information in the Session object), so the firs page would be the logon one. – vtortola Nov 28 '11 at 17:29

2 Answers2

2

This is a great question. My experience has been to more you try to interfere with ASP.NET internals the more pain you cause.

Have you considered using serviceAutoStartProviders to keep your app hot?

ASP.NET 4.0: How to use application warm-up class

rick schott
  • 21,012
  • 5
  • 52
  • 81
  • That is interesting. But, what happens if the warm-up logic takes long? there is nobody answering requests in the meantime? Maybe keep to application pools would be the solution, but there is any way to tell IIS to keep one always alive? – vtortola Nov 28 '11 at 20:00
  • 1
    It's hard to say, these types of things are on a case by case basis. It will take some trial and error before you get it just right. – rick schott Nov 28 '11 at 20:29
0

You should be using IHttpAsyncHandler to return your slow loading big data. Walkthrough: Creating an Asynchronous HTTP Handler

During processing of an asynchronous HTTP handler, ASP.NET puts the thread that would ordinarily be used for the external process back into the thread pool until the handler receives a callback from the external process. This can prevent thread blocking and improve performance, because only a limited number of threads can be executing at the same time.

You should have no problem taking 12 seconds to return from an IHttpAsyncHandler. In fact you should be able to easily go up to about 60 seconds, without worry. Beyond that you may need to implement some type of long-polling system to monitor system status.

The IHttpAsyncHandler can return XML/JSON or whatever you like. You can consume the IHttpAsyncHandler via ajax in javascript (maybe jQuery.ajax) or even server side if you want using an HttpWebRequest (some sample C# code: Calling remote JSON webservice).

Community
  • 1
  • 1
BenSwayne
  • 16,810
  • 3
  • 58
  • 75
  • I am already using AsyncController in MVC, and I have already an IAsyncResult that represents the IOCP, what I want is tell two or more requests to wait to that IOCP without a spin wait, because the data is common and I don't want to retrieve it more than one time. – vtortola Dec 06 '11 at 10:48
  • @vtortola Then you should be caching the data in memory in your MVC app and serving the cached copy. That means you could have multiple IAsyncResults waiting for the cache to retrieve the data only one time. This question should really be about slow large data application architecture, not about IOCP. You don't want to be messing with the underpinnings of the server especially if you can accomplish your task via common means. – BenSwayne Dec 06 '11 at 16:18