Connect Apps with WCF: Listing 1: C#: Use a Callback Reference in a Service

Extension methods let you add logical functionality to existing classes. You control available extension methods through references and namespace using statements.

class ProductServiceWithCallbacks : 
   IProductService2
{
   static List<IProductServiceCallback> 
      m_Subscribers = 
      new List<IProductServiceCallback>();

   List<Product.Entities.ConsumerProduct> 
      IProductService2.GetProducts()
   {
      ...
   }

   void IProductService2.SubscribeProductChanges()
   {
      IProductServiceCallback client = 
         OperationContext.Current.
         GetCallbackChannel<IProductServiceCallback>();
      lock (m_Subscribers)
      {
         m_Subscribers.Add(client);
      }
   }

   void IProductService2.UnsubscribeProductChanges()
   {
      IProductServiceCallback client =
         OperationContext.Current.
         GetCallbackChannel<IProductServiceCallback>();
      lock (m_Subscribers)
      {
         m_Subscribers.Remove(client);
      }
   }

   static void NotifyClientsProductChanged(
      ConsumerProduct product)
   {
      lock (m_Subscribers)
      {
         foreach (IProductServiceCallback 
            client in m_Subscribers)
         {
            client.ProductChanged(product);
         }
      }
   }
}
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.