Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Dynamically Generate a WCF Proxy

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

namespace DynamicWcfProxyClient
{
   class Program : IMessageCallback 
   {
      static void Main(string[] args)
      {
         new Program().Start();
      }

      private void Start()
      {
         EndpointAddress endpointAddress = new EndpointAddress(Configuration.CreateServerUriString());
         Binding binding = Configuration.CreateBinding();

         Console.WriteLine("Using a normal channelfactory.");
         SendMessageUsingChannelFactory(binding, endpointAddress);

         Console.WriteLine("Using a duplex channelfactory with callbacks.");
         SendMessageUsingDuplexChannelFactory(binding, endpointAddress);

         Console.ReadLine();
      }

      private void SendMessageUsingDuplexChannelFactory(Binding binding, EndpointAddress endpointAddress)
      {
         DuplexChannelFactory<IMessageSenderWithCallBack> duplexChannelFactory = new DuplexChannelFactory<IMessageSenderWithCallBack>(this, binding, endpointAddress);
         IMessageSenderWithCallBack duplexMessageSender = duplexChannelFactory.CreateChannel(endpointAddress);
         duplexMessageSender.Subscribe();
         for (int messageCounter = 0; messageCounter < 10; messageCounter++)
         {
            Console.WriteLine("Sending message to server");
            duplexMessageSender.ShowMessage(string.Format("Message {0}", messageCounter));
         }
         duplexMessageSender.Unsubscribe();
         ((IChannel)duplexMessageSender).Close();
      }

      private void SendMessageUsingChannelFactory(Binding binding, EndpointAddress endpointAddress)
      {
         ChannelFactory<IMessageSender> channelFactory = new ChannelFactory<IMessageSender>(binding, endpointAddress);
         IMessageSender messageSender = channelFactory.CreateChannel(endpointAddress);
         for (int messageCounter = 0; messageCounter < 10; messageCounter++)
         {
            Console.WriteLine("Sending message to server");
            messageSender.ShowMessage(string.Format("Message {0}", messageCounter));
         }
         ((IChannel)messageSender).Close();
      }

      public void OnMessageSend(string messageSend)
      {
         Console.WriteLine("Retrieved Callback {0}", messageSend);
      }
   }
}

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.hinttech.nl
Netherlands Netherlands
Patrick Kalkman is a senior Software Architect with more than 20 years professional development experience. He works for Hinttech where he develops state of the art web applications.

Patrick enjoys writing his blog. It discusses software architectures using semantic web technologies. Patrick can be reached at pkalkie@gmail.com.
 
Published Windows 8 apps:
 
Published Windows Phone apps:
 
Awards:

Best Mobile article of March 2012
Best Mobile article of June 2012
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 8 Jan 2011
Article Copyright 2010 by Patrick Kalkman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid