In this posting, let us share our experience of working with WCF (Windows Communication Foundation), an API that got introduced with .Net Framework 3.0. Earlier we used to work with various features like
- Web Services
- .Net Remoting
- Distributed Transactions and
- Message Queues.
WCF, designed based upon SOA (Service Oriented Architecture) supporting distributed computing, address the above features as a single entity.
The major advantage of WCF is, it supports synchronous, asynchronous and REST style communications utilizing various protocols like http, netnamedpipebinding etc. We used netnamedpipebinding protocol for on-machine communication where the end point will be defined based on protocol type.
Various steps to create a WCF Service are:
Step 1: Create Interface Class and define the class with Service Contract attribute and methods with Operation Contract attribute.
[ServiceContract(Namespace = "http://CalculatorService", SessionMode = SessionMode.Required)]
public interface ICalculator
double Add(double n1, double n2);
double Subtract(double n1, double n2);
double Multiply(double n1, double n2);
double Divide(double n1, double n2);
Step 2: Create Service Class that implements the interface defined with Service Contract attribute and define the class with Service Behavior attribute. The Service Behavior attribute have two different parameters such as Instance Context mode and Concurrency mode. In Instance Context mode we have to specify the Service instance is single ton or it will be created for each call or each session. In Concurrency mode we have to specify whether it is single threaded or multi threaded.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class CalculatorService : ICalculator
public double Add(double n1, double n2)
return n1 + n2;
public double Subtract(double n1, double n2)
return n1 - n2;
public double Multiply(double n1, double n2)
return n1 * n2;
public double Divide(double n1, double n2)
return n1 / n2;
Step 3: Host the Service as Console based application using the ServiceHost.
internal static ServiceHost myServiceHost = null;
internal static void StartService()
myServiceHost = new ServiceHost(typeof(CalculatorService.CalculatorService));
catch (Exception ex)
internal static void StopService()
if (myServiceHost.State != CommunicationState.Closed)
catch (Exception ex)
Step 4: Define the configuration in the host environment with protocol name and endpoint address.
<service name="CalculatorService.CalculatorService" behaviorConfiguration="metadataSupport">
<add baseAddress="net.pipe://localhost/CalculatorService" />
<endpoint address="" binding="netNamedPipeBinding" contract="CalculatorService.ICalculator" />
<endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
<serviceDebug includeExceptionDetailInFaults="true" />
Step 5: This step helps in generating the proxy object. Before generating the proxy, first run the service using command prompt. The following command will generate the service proxy object and configuration file.
Svcutil net.pipe://localhost/CalculatorService /config:App.Config
The following will be the output
C:\Documents and Settings\WCFDeveloper >svcutil net.pipe://localhost/CalculatorService /config:App.Config
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.648]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from 'net.pipe://localhost/CalculatorService ' using
WS-Metadata Exchange. This URL does not support DISCO.
C:\Documents and Settings\WCFDeveloper \CalculatorService.cs
C:\Documents and Settings\WCFDeveloper \App.Config
Client will connect to a service using a Proxy Object, which is connected to the specified endpoint of the service. Both the file need to be added in the client project.
Step 6: Calling the Service from client through the service proxy object.
Here CalculatorClient is the generated service proxy object.
CalculatorClient client= new CalculatorClient();;
private void uxAdd_Click(object sender, EventArgs e)
Double num1 = Convert.ToDouble(uxNumber1.Text.Trim());
Double num2 = Convert.ToDouble(uxNumber2.Text.Trim());
uxResult.Text = client.Add(num1, num2).ToString();
Now both the client and service are ready for deployment.