Sample WCF Server and Client program
Post date: Mar 2, 2011 12:26:05 PM
Server Side coding:
1. Start-> Program-> VS2008
2. Open File-> New -> Project-> Project Type = WCF and Template = WCF Service Library
Give the Project name as Calculator -> Select Location to save -> OK
3.Now you can find IService1.cs and Service1.cs, delete both that boiler plate files.
4. Click Add-> Class-> ICalculator.cs
Now change the class into interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCFCalculator
{
[ServiceContract] //WCF contract for interface
public interface ICalculator
{
[OperationContract] //Service Contract with WCF
float add(int a, int b);
[OperationContract]
float sub(int a, int b);
[OperationContract]
float div(int a, int b);
[OperationContract]
float mul(int a, int b);
}
}
5. Then We need implement that interface, So Click Add->Class->Calculator.cs
It is the server side implementation of code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WCFCalculator
{
class Calculator :ICalculator
{
#region ICalculator Members
public float add(int a, int b)
{
return (a + b);
}
public float sub(int a, int b)
{
return (a - b);
}
public float div(int a, int b)
{
if (b != 0)
{
return a / b;
}
else
{
return 0;
}
}
public float mul(int a, int b)
{
return (a * b);
}
#endregion
}
}
6. Then open App.config in the solution explorer , the you could find that boiled code Service1 configuration, now you need to replace with your Calculator configuration.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WCFCalculator.Calculator" behaviorConfiguration="WCFCalculator.CalculatorBehavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/Design_Time_Addresses/WCFCalculator/Calculator/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="wsHttpBinding" contract="WCFCalculator.ICalculator">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFCalculator.CalculatorBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
7. Then Run it by pressing F5, then you will following output.
Now Server is ready for serve.
Open Visual Studio 2008 Command Prompt-> and type following command
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8731/Design_Time_Addresses/WCFCalculator/Calculator/
then you will get generatedProxy.cs, app.config as output
Way to write the client code to invoke the server:
1. Add new project by clicking the Add-> New Project -> ADD Windows application.(CalculatorApps)
2. Now rightclick (CalculatorApps)-> Add Exsisting item-> Add generatedProxy.cs and app.config(Which we are generated in preceding step)
3. Now right Click Add reference-> .NET(TAB)-> Add System.ServiceModel reference to your client project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
namespace CalculatorApps
{
public partial class Form1 : Form
{
CalculatorClient client;
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtValues1.Text);
int b = Convert.ToInt32(txtValues2.Text);
MessageBox.Show("The result is "+ client.add(a,b).ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
//Now we used WSHttpBinding Binding
WSHttpBinding binding = new WSHttpBinding();
//End point address for remote access
EndpointAddress address = new EndpointAddress("http://localhost:8731/Design_Time_Addresses/WCFCalculator/Calculator");
// create the object for client proxy
client = new CalculatorClient(binding, address);
}
}
}
like wise you have to do min, mul, div operation by adding buttons.
We done it.