Creating a WCF Channel with ChannelFactory

In the previous posts on WCF I constructed and configured a simple WCF service; EchoService. Now it’s time to consume the service in a basic console application. This is done by creating a channel on the client to the service. In this example this is done by using ChannelFactory. Once the channel is in place we can send messages back and forth to the service.

using System;
using System.ServiceModel;
using EchoClientConsole.EchoServiceReference;
 
namespace EchoClientConsole
{
    internal class Program
    {
        private static void Main()
        {
            var channelFactory =
                new ChannelFactory<IEchoService>(
                    "WSHttpBinding_IEchoService" // config endpoint name
                    );
 
            IEchoService channel = channelFactory.CreateChannel();
            EchoMessage result = channel.Echo(
                new EchoMessage
                    {
                        Text = "Hey "
                    });
 
            Console.WriteLine("Service responded at {0}: {1}",
                              result.Invoked,
                              result.Text);
            Console.ReadLine();
 
            ((IClientChannel)channel).Close();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>