Page view counter

Ajax push / async web service

Last post 04-13-2007 11:27 AM by Henk de Koning. 1 replies.

Sort Posts:

  • Ajax push / async web service

    04-12-2007, 11:20 AM
    • Loading...
    • Joink!
    • Joined on 02-09-2006, 2:06 PM
    • Posts 5
    • Points 20

    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 :)

  • Re: Ajax push / async web service

    04-13-2007, 11:27 AM

    Hi Joink!

    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.

    3) If you want total control, implement your web service in WCF. It is not too difficult to setup custom serialization and you can simply reuse the JSON serializer from System.Web.extensions (JavaScriptSerializer).

    HTH,

     -- Henkk

Page 1 of 1 (2 items)