WCF - Creating a Service and Client in ASP.NET 3.5 and C#
Introduction
The Windows Communication Foundation (WCF) is great for allowing applications to interact and integrate with one another across platforms. WCF is a powerful introduction to the .NET Framework, aimed at filling gaps left by such implementations as Web Services, and this article will guide you through creating a simple WCF Service, and then implementing that Service with a Client. We can host the Service on the same machine as the Client, which is great for testing. This article will result in us creating a simple calculator service, that we can consume in a client. The Client will call the service to use the simple calculator tasks of addition, subtract, multiply and divide.
What we will learn in this article:
- How to Create a WCF Service in Visual Studio.NET 2008;
- How to consume the WCF Service in a Client Console application.
Please Note:
To create this example WCF Service, we will be using Visual Studio.NET 2008 with ASP.NET 3.5, which comes with WCF built-in. However, this article will make use of the CMD Shell from the Windows SDK to build the client application, which can be downloaded from Microsoft here
Getting Started with our WCF Service
To begin, we will start up Visual Studio and create a New Project, Console Application. We will start with our WCF Service. Name it Service1, and choose a location. When the project opens, we should see something like this:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProgrammingHelp.WCFService1
{
{
{
}
Our next step is to add a reference to the ServiceModel.dll. To do this, right-click on the References folder in Solution Explorer, and choose Add Reference. In the ASP.NET Tab, scroll down to System.ServiceModel, select it and click Ok. Now add a using directive:
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Now we can create our interface for the calculator, which includes our Operation Contracts. The contract defines the functionality offered by the Service, and describes how to use it. Add the following to the namespace:
public interface ICalculator
{
Double Add(Double n1, Double n2);
[OperationContract]
Double Subtract(Double n1, Double n2);
[OperationContract]
Double Multiply(Double n1, Double n2);
[OperationContract]
Double Divide(Double n1, Double n2);
Now to implement this contract, we create the methods in a class which inherits our interface:
{
{
Console.WriteLine("Call made: Add({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
public Double Subtract(Double num1, Double num2)
{
Console.WriteLine("Call made: Subtract({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
public Double Multiply(Double num1, Double num2)
{
Console.WriteLine("Call made: Multiply({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
public Double Divide(Double num1, Double num2)
{
Console.WriteLine("Call made: Divide({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
Now we will create the Main object inside the Program class. This will add the Service EndPoint and ultimately initialize our Service. Here, we will use the ServiceMetadataBehavior class, which is in the System.ServiceModel.Description namespace. We will need to also add a reference to this.
{
{
ServiceHost localHost = new ServiceHost(typeof(CalculatorService), baseAddr);
try
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
localHost.Description.Behaviors.Add(smb);
localHost.Open();
Console.WriteLine("Service initialized.");
Console.WriteLine("Press the ENTER key to terminate service.");
Console.WriteLine();
Console.ReadLine();
localHost.Close();
catch (CommunicationException ex)
{
localHost.Abort();
Now our service is pretty much complete and ready to be consumed. Our Entire Service code looks like this:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ProgrammingHelp.WCFService1
{
public interface ICalculator
{
Double Add(Double n1, Double n2);
[OperationContract]
Double Subtract(Double n1, Double n2);
[OperationContract]
Double Multiply(Double n1, Double n2);
[OperationContract]
Double Divide(Double n1, Double n2);
class CalculatorService : ICalculator
{
{
Console.WriteLine("Call made: Add({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
public Double Subtract(Double num1, Double num2)
{
Console.WriteLine("Call made: Subtract({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
public Double Multiply(Double num1, Double num2)
{
Console.WriteLine("Call made: Multiply({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
public Double Divide(Double num1, Double num2)
{
Console.WriteLine("Call made: Divide({0},{1})", num1, num2);
Console.WriteLine("Answer: {0}", answer);
return answer;
class Program
{
{
ServiceHost localHost = new ServiceHost(typeof(CalculatorService), baseAddr);
try
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
localHost.Description.Behaviors.Add(smb);
localHost.Open();
Console.WriteLine("Service initialized.");
Console.WriteLine("Press the ENTER key to terminate service.");
Console.ReadLine();
localHost.Close();
catch (CommunicationException ex)
{
localHost.Abort();
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
We will create our Client in a new Project, but within the same Solution as the Service. To do this, right-click the Solution in Solution Explorer, and choose Add > New Project.. Console Application. Name it ClientApp. As with the Service, add a reference to the System.ServiceModel.dll and a using directive.
Next, run the service from Visual Studio (hit F5), and then we need to goto the Start Menu > All Programs > Microsoft Windows SDK > CMD Shell. If you do not have this, then you will need to download the Windows SDK at the link at the top of this article.
You should have something like this:
[Click to see full-size]
Now we want to navigate to our ClientApp folder using cd (Change Directory). Example, input cd "C:\Users\xxxxxx\Visual Studio 2008\Documents\Consoles\WCFService1\ClientApp" and hit Enter:
[Click to see full-size]
Finally, enter the following to generate the necessary files for our Client and then hit Enter:
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/WCFService1
This will generate our Client files.
[Click to see full-size]
You can now close the CMD Shell window. Navigate to your ClientApp folder and verify that the files were created in the correct location. You should now have app.config and generatedProxy.cs.
We can now also terminate the Service and go back into Visual Studio. In Solution Explorer, right-click the ClientApp project and choose to Add > Existing Item. Enter * in the File name box and hit enter. There, choose to add the generatedProxy.cs and the app.config we just created. The App.config file should look something like this:
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">