views:

791

answers:

2

Hi, I'm trying to use WCF to implement a comet style server push to an ajax web application.

In my WCF service, I've implemented a WaitForEvents method that calls Monitor.Wait to suspend the thread until new data arrives. At that point, the Monitor is pulsed, and the method returns the new data which closes the comet style request.

The request is made again when this happens.

Currently, this works fine but I noticed that WCF needs to create a new thread for each connected user. This is probably because the thread cannot be returned to the threadpool until data comes in, and so each connected user requires a new thread.

I want to make this implementation more efficient by having one thread service multiple connections. If I were to use a socket, this could be done by leaving the socket open and returning the thread to the thread pool first. When new data arrives, it will be delivered by another thread and we can write the new data directly to the socket and close it.

Does anybody know how this can be done via WCF?

I have been looking at "Push-Style Streaming" http://msdn.microsoft.com/en-us/library/bb472551.aspx and they mention that "WCF implements a "pull" model in which the application code (the service) returns an instance of Stream and relies on the lower-level infrastructure to pull data from this stream and write it out to the network." but I cant find any examples of this one the website.

Many thanks in advance!

+2  A: 

Check out nComet

It's not using WCF, but I believe the author is working on a version that uses WCF. Contact him via codeplex and ask him :-)

"nComet is a .NET implementation of the Comet (reverse-AJAX push) architecture. This server-side pipeline uses long-lived client-initiated HTTP connections to push messages to the client. Once the client receives a response, it immediately opens another HTTP request, which the server holds until a message is ready. This architecture allows the server to push dynamic html/xml/json/etc to the browser, rather than the browser polling the server.

This project is scoped to the .NET server-side architecture, initially providing a HttpListener (for a custom host communicating with HTTP.SYS directly) as well as a ASP.NET implementation, where the ASP.NET implementation can be hosted inside IIS as well as an external process. The library will simplify the implementation of common message patterns such as pushing the latest data, as well as sync. Example code and links to multiple client-side javascript implementations will also be provided."

Joel Martinez
+1  A: 

Here is a sample project for ASP.NET WCF

Zuuum