title: How to clear the outgoing message buffers at the Server?
body: <p>I've written a service using <code>PollingDuplexHttpBinding</code> which has a Silverllight client that consumes it. My Service basically has a given number of clients sending their data (quite often, every second, and the data is quite large, each call is around 5KB) to the service, and also listening for new data sent by other clients to the service to be routed to them, very similar to a chat room architecture. </p>
  
  <p>The problem I'm noticing is that when clients connect to the service over the internet, after a few minutes the service's response becomes slow and the replies become lagged. I've come to the conclusion that when the service hosts upload capacity is reached (internet upload speed, on the server its about 15KB/s only), the messages sent by other clients are buffered and processed accordingly when there is bandwidth available. I'm wondering how exactly can I limit the seize of this buffer that the service uses to store the received messages from the clients? It's not so critical that my clients get all the data, but rather that they get the latest data sent by others, so real time connectivity is what i'm looking for at the cost of guaranteed delivery. </p>
  
  <p>In short I want to be able to clean my queue/buffer at the service whenever it fills up, or a certain cap is reached and start filling it again with the received calls to get rid of the delay. How do I do this? Is the <code>MaxBufferSize</code> property what I need to decrease on the service side as well as on the client side? Or do I need to code this functionality in my service? Any ideas?</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is my service architecture:</p>
  
  <pre><code>//the service
  [ServiceContract(Namespace = "", CallbackContract = typeof(INewsNotification))]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
  public class NewsService
  {
  
  
  private static Dictionary&lt;IChatNotification, string&gt; clients = new Dictionary&lt;IChatNotification, string&gt;();
  private ReaderWriterLockSlim subscribersLock = new ReaderWriterLockSlim();
  
  [OperationContract(IsOneWay = true)]
  public void PublishNotifications(byte[] data)
  {
              try
              {
                  subscribersLock.EnterReadLock();
                  List&lt;INewsNotification&gt; removeList = new List&lt;INewsNotification&gt;();
                  lock (clients)
                  {
                      foreach (var subscriber in clients)
                      {
                          if (OperationContext.Current.GetCallbackChannel&lt;IChatNotification&gt;() == subscriber.Key)
                          {
                              continue;
                          }
                          try
                          {
                              subscriber.Key.BeginOnNotificationSend(data, GetCurrentUser(), onNotifyCompletedNotificationSend, subscriber.Key);
  
                          }
                          catch (CommunicationObjectAbortedException)
                          {
                              removeList.Add(subscriber.Key);
                          }
                          catch (CommunicationException)
                          {
                              removeList.Add(subscriber.Key);
                          }
                          catch (ObjectDisposedException)
                          {
                              removeList.Add(subscriber.Key);
                          }
  
                      }
                  }
  
                  foreach (var item in removeList)
                  {
                      clients.Remove(item);
                  }
              }
              finally
              {
                  subscribersLock.ExitReadLock();
              }
          }
  
  }
  
  
  //the callback contract
  [ServiceContract]
  public interface INewsNotification
  {
         [OperationContract(IsOneWay = true, AsyncPattern = true)]
         IAsyncResult BeginOnNotificationSend(byte[] data, string username, AsyncCallback callback, object asyncState);
         void EndOnNotificationSend(IAsyncResult result);
  }
  </code></pre>
  
  <p>The service config:</p>
  
  <pre><code>  &lt;system.serviceModel&gt;
      &lt;extensions&gt;
        &lt;bindingExtensions&gt;
          &lt;add name="pollingDuplex" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement, System.ServiceModel.PollingDuplex, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /&gt;
        &lt;/bindingExtensions&gt;
      &lt;/extensions&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=""&gt;
  
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
            &lt;serviceThrottling maxConcurrentSessions="2147483647" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;bindings&gt;
        &lt;pollingDuplex&gt;
  
          &lt;binding name="myPollingDuplex" duplexMode="SingleMessagePerPoll" 
                   maxOutputDelay="00:00:00" inactivityTimeout="02:00:00" 
                   serverPollTimeout="00:55:00" sendTimeout="02:00:00"  openTimeout="02:00:00" 
                    maxBufferSize="10000"  maxReceivedMessageSize="10000" maxBufferPoolSize="1000"/&gt;
  
        &lt;/pollingDuplex&gt;
      &lt;/bindings&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
      &lt;services&gt;
        &lt;service name="NewsNotificationService.Web.NewsService"&gt;
          &lt;endpoint address="" binding="pollingDuplex" bindingConfiguration="myPollingDuplex" contract="NewsNotificationService.Web.NewsService" /&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
    &lt;/system.serviceModel&gt;
      &lt;system.webServer&gt;
          &lt;directoryBrowse enabled="true" /&gt;
      &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>The client will call the service typically between 500ms-1000ms periods like this:</p>
  
  <p><code>_client.PublishNotificationAsync(byte[] data);</code></p>
  
  <p>and the callback will notify the client of notifications sent by other clients:</p>
  
  <pre><code>void client_NotifyNewsReceived(object sender, NewsServiceProxy.OnNewsSendReceivedEventArgs e)
          {
                  e.Usernamer//WHich client published the data
                  e.data//contents of the notification
          }
  </code></pre>
  
  <p>So to recap, when the number of clienst increase, and the service hosts upload speed over the internet is limited, the messages sent out by the service to the subscribers get buffered somewhere and get processed in a queue, which is whats causing the problem,
  I don't know where these messages are buffered. In a LAN the service works fine because the server has an upload speed equal to the its download speed (for 100KB/s of incoming calls, it sends out 100KB/s of notifications). Where are these messages getting buffered? And how can I clear this buffer?</p>
  
  <p>I did something experimental to try and see if the messages are buffered at the service, I tried calling this method on the client, but it always returns 0, even when one client is still in the process of receiving notifications that somebody else sent 4-5 minutes ago:</p>
  
  <pre><code>[OperationContract(IsOneWay = false)]
  public int GetQueuedMessages()
  {
  
              return OperationContext.Current.OutgoingMessageHeaders.Count();
  }
  </code></pre>