Windows Communication Foundation (WCF)

Windows Communication Foundation is a mature .NET Framework technology for creating Web Service applications. It can be implemented with a focus on high compatibility as well as on tight integration for high performance and secure solutions. WCF supports many communication and security standards out of the box and using them is usually just a question of turning these features on or off in a configuration file.

In this section, we will see how to create a simple WCF service application. The application will use the default HTTP communication. To test this application, we will use the built in WCF Test Client. We will also see how to create our own client application from scratch. This should give you a basic overview of the WCF technology. Later, we will look at a more complex WCF application with a database connection.

Simple WCF Service Application

To create a new WCF Service Application, start Visual Studio, go to “File” menu and select “New” > “Project”. A new dialog window appears. Select “WCF” on the left and then “WCF Service Application”, type the name of the application and click “OK”:

New Project 1

A new WCF Service application gets created. There are already IService1.cs and Service1.svc files added to the project:

Solution Explorer 1

The first file, IService1.cs, contains the service “contract”. The contract describes all the operations the client can perform on the service.


The second file, Service1.svc, provides an implementation for the contract:


Looking at these two files, you can see that there are two operations already available. The first one, GetData, takes an integer parameter and returns a string in a format “You entered: {integer}”. The second one, GetDataUsingDataContract, shows how to accept and return a more complex data type.

You can test this “default” service implementation right away by opening the “Service1.svc” file and pressing F5. It is important that you make sure the “Service1.svc” file is opened, otherwise you will not get the built-in WCF Test Client:

WCF Test Client 1

To test the “GetData” operation, for example, select it on the left and then enter the value on the right. Once you click the “Invoke” button, you should see the operation result:

WCF Test Client 2

You can test the second operation similarly.

We will now try to change the default application to our own SimpleWcfClient application. Let us start with renaming the Service1 and IService1 to SimpleService and ISimpleService. Make sure you rename not only the files in the solution explorer but also the classes and the markup in the SimpleService.svc file. To show the markup in the SimpleService.svc file, right-click it in the solution explorer and select “View Markup”.

You should now end up with a project structure looking like this:

Solution Explorer 2

Our simple application will have a single operation called SayHello. SayHello takes a name as a string and returns a greeting as another string. To define this contact, change the content of the ISimpleService file as follows:

using System.ServiceModel;

namespace MyCSharpDotNet.SimpleWcfService
    public interface ISimpleService
        string SayHello(string name);

It is important to mark the interface with the ServiceContract attribute and each operation with the OperationContract attribute.

To implement this contract, change the content of the SimpleService.svc.cs file accordingly:

namespace MyCSharpDotNet.SimpleWcfService
    public class SimpleService : ISimpleService
        public string SayHello(string name)
            return string.Format("Hello: {0}", name);

Now everything is ready to test our new application. Again, make sure you have the SimpleService.svc file open and hit F5. In the WCF Test Client, test the SayHello method:

WCF Test Client 3

One everything works great in the test client, we can go ahead and create our own client application.

Keep the service application with the WCF Test Client window running and open a new instance of Visual Studio. Create a new Console Application by going to “File” menu and selecting “New” > “Project”. A new dialog window appears. Select “Visual C#” on the left and then “Console Application” on the right. Type the name of the application and click “OK”:

New Project 2

Now we need to add a new reference to the web service application we have just created. To get the URL of the application, simply go to the WCF Test Client window we left there running and copy the URL to the clipboard:

WCF Test Client 4

To add a new reference to our Console application, right-click the project name in the Solution Explorer and select “Add” > “Service Reference…” In the new window, paste in the URL and type the name of the reference:


Once you have the reference added, calling the service is very easy. Just add these three lines to the Main method of the Program.cs file in our new Console application:

SimpleServiceClient client = new SimpleServiceClient();

You should see this web service call result when you run the console application by hitting F5:

Client Running

As we have discussed earlier, there are many configuration options available with WCF. All of these options can be set up in the config files, both, on the server and the client. In this simple application, we have kept them at their defaults.

The complete Simple WCF Service Application is available for download.

Advanced WCF Service Application

Much like the Simple WCF Service Application, the advanced one shows how to create a separate server service and a client. The service application demonstrates how to return lists of complex types from the database. The client application uses the WPF technology to display these lists after getting them with an asynchronous call.

To learn more, including a few possible WCF configuration options, feel free to examine the Advanced WCF Service Application source code.

Continue to: ASP.NET Web API

Go up to: Web Service Applications

Should you have any questions or found a mistake that needs correcting, feel free to send an email to: info [at] mycsharp [dot] net

Advertisements :