Last post Apr 13, 2007 04:27 PM by Henk de Koning
Apr 12, 2007 04:20 PM|Joink!|LINK
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 :)
ajax push json webservice
Apr 13, 2007 04:27 PM|Henk de Koning|LINK
A couple of remarks:
1) if you want to return JSON from a web service, you can mark it as a script service (ScriptService/ScriptMethod attributes). This should cause it to spit out JSON
2) the async framework was indeed largely designed for pages and not so much for web services. The only support I can think of is implementing an async handler (IHttpAsyncHandler). This would involve replacing the .asmx script map with one that points to
your implementation and then load and invoke the asmx handler from your own background thread. But keep in mind that you'll block a thread regardless, unless your status extraction mechanism has async support itself.