title: WCF comet and threads
body: <p>I'm trying to use WCF to implement a comet style server push to an ajax web application.</p>
  
  <p>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.</p>
  
  <p>The request is made again when this happens.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Does anybody know how this can be done via WCF?</p>
  
  <p>I have been looking at "Push-Style Streaming" <a href="http://msdn.microsoft.com/en-us/library/bb472551.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb472551.aspx</a> 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.</p>
  
  <p>Many thanks in advance!</p>