Web application developer

Creating a WCF client


Written by: willem | Feb 5 2014 2:17 PM

In my previous post I showed how to make a simple WCF service. In this post I will show you how to use this service without using a proxy. A proxy is used to connect to the service if you don't have access to the code of the service. Since we created the service ourselves it is sufficient to make a reference only.

This post is the third in a series of posts.

The client will connect to the same service I described in my earlier post. You can also download the solution here.

Start visual studio as administrator and open the solution. Right click the solution in the solution explorer and click “add” and then “add new” project.


Select WPF application under Windows and name give the project a name.


Creating forms in WPF is not in the scope of this article, so I assume you know how to create a form like this. You can also open the markup of the MainWindow.xaml and overwrite the code with the one underneath.



Next open the code behind of the MainWindow.xaml and change the code to this:

using MessageService;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Windows;

namespace WcfClients
	/// Interaction logic for MainWindow.xaml
	public partial class MainWindow : Window
		public MainWindow()

		private void Button_Click(object sender, RoutedEventArgs e)
			EndpointAddress endpoint = new EndpointAddress("http://localhost:8733/MessageService");
			Binding binding = new BasicHttpBinding();
			ChannelFactory factory = new ChannelFactory(binding, endpoint);
			IMessageService service = factory.CreateChannel();

			MessageService.Message message = new MessageService.Message();
			message.Reverse = true;
			message.StringValue = InputTextBox.Text;

			OutputTextBox.Text = service.ProcessMessage(message);

In the button click event the following steps are taken:

  • An endpoint is created. In an endpoint the location of the service is defined. This is the base address from the app.config in the service project.
  • A binding is created. In our case we create a BasicHttpBinding. Meaning that we use the http protocol to connect to the service.
  • A ChannelFactory is used to create an instance of the IMessage service.
  • A message is created with the input textbox’s value as StringValue. Also the reverse property is set to true.
  • Finally we send the message to the ProcessMessage() method and write the result to the output textbox.

Next we need to add two references. One to the ServiceModel library which is used for connecting to the service and one to the service itself. The reference to the service is basically to be able to use the interface IMessageService and the Message class. You could also put those files in a shared project and refer from the service and the client to that project.

To add the references right click “references” under the client project and click “Add Reference”.


Search for “System.ServiceModel” under “Framework'” and tick it.


Next select “MessageService” under “Projects” and click “OK”


Since we want the client to connect to the service, both need to run when you debug the solution. To do that right click the solution and click “properties”.


Under startup project, you will see your two projects. Make sure both are set to “Start” in the action column and click “OK”.


Now you can run the solution. Press F5 or click the debug button. Visual studio will startup the service and the WCF test client like before. It will also start the newly created client. Send some text and see if it comes back reversed.


That’s it. You can download the complete solution here.


No Comments

Add a Comment

About Me

I'm a web application developer specializing in asp.net. I have skills in asp.net, C#, html5, javascript, asp.net mvc, design patterns and more.

more about me

Random background module

This module for Orchard CMS lets you upload a list of images. For every page it chooses one randomly and sets it a the background image.

Go to project page on CodePlex
Download module

The Monack Framework

This open source framework makes it easy to build and develiver custom web applications. Unfortunately I can't spent much time on it so it is still work in progress. 

More about MonackFr
Project page at CodePlex


Since I decided to create this website in Orchard and didn't know anything about
it when I started, I also post my experiences with this CMS.