Click here to Skip to main content
12,507,845 members (55,589 online)
Click here to Skip to main content

Stats

77.5K views
2.4K downloads
45 bookmarked
Posted

Dynamically Generate a WCF Proxy

, 8 Jan 2011 CPOL
Save time by using a dynamically generated WCF proxy
using System;
using System.Collections.Generic;
using System.ServiceModel;
using SharedInterface;

namespace DynamicWcfProxyServer
{
   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
   public class Server : IMessageSender, IMessageSenderWithCallBack
   {
      private readonly List<IMessageCallback> subscribers = new List<IMessageCallback>();

      public void ShowMessage(string messageToShow)
      {
         Console.WriteLine(messageToShow);

         foreach (IMessageCallback subscriber in subscribers)
         {
            Console.WriteLine("Sending callback {0}", messageToShow);
            subscriber.OnMessageSend(messageToShow);
         }
      }

      public bool Subscribe()
      {
         try
         {
            IMessageCallback messageCallback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
            if (!subscribers.Contains(messageCallback))
            {
               subscribers.Add(messageCallback);
            }
            return true;
         }
         catch (Exception error)
         {
            Console.WriteLine(error);
            return false;
         }
      }

      public bool Unsubscribe()
      {
         try
         {
            IMessageCallback messageCallback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
            if (subscribers.Contains(messageCallback))
            {
               subscribers.Remove(messageCallback);
            }
            return true;
         }
         catch (Exception error)
         {
            Console.WriteLine(error);
            return false;
         }
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Patrick Kalkman
Architect http://www.simpletechture.nl
Netherlands Netherlands
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He works for SimpleTechture where he helps teams develop state of the art web applications.

Patrick enjoys writing his blog. It discusses agile software development. Patrick can be reached at patrick@simpletechture.nl.

Published Windows 8 apps:

Published Windows Phone apps:

Awards:

Best Mobile article of March 2012
Best Mobile article of June 2012

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 8 Jan 2011
Article Copyright 2010 by Patrick Kalkman
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid