I am currently creating a vista gadget which uses a asp.net web service to fetch data. The web service methods are marked with the ScriptMethod attribute in order to automatically generate JSON and not XML. I access them using the XHR object directly, setting the 'Content-Type' header so they will return JSON.
One of the web service methods returns status information from the server, and this data changes occasionally. I have created a polling mechanism to update the status, but I don't think polling is optimal since I keep hitting the server with requests when there is no status change, and the status on the client is not updated instantly when the status changes on the server.
I therefore created a push mechanism (similar to what is done in office communicator web access i guess) which basically does a request to the web service, which in turn waits until the status on the server changes before returning the response. It will check if the server status has changed, if it has changed it will immediately return the new response. If not, it will do a thread.sleep(1000) and check again (a limited number of times, or until the status changes, before returning the old status).
The problem I now face is that the request will hog a thread from the thread pool, until it returns a response to the client. I would like this to be done asynchronously, so when the request to the web service is made, it will start the server side polling, and release the thread to the thread pool. When the status changes, or it has polled for a long enough time, it should fire up a new thread and return the response to the client.
I have briefly looked at the new async framework in asp.net 2.0, but haven't really found a solution to my problem. Just as a practice, i tried to create a aspx page with the async flag set to true, and used the MethodAsync/MethodCompleted approach. The problem i faced here was since my web service returns a IDictionary, the result I got when the MethodCompleted event was fired was of type DataSet (and it didn't contain any data - methods with return type string worked OK). I would like it to return a JSON object literal, just as it does when called from the XHR object.
Any input on how I could go about solving this problem is greatly appreciated :)