This tutorial will show you how to create a simple WCF-to-WCF Windows-based ditributed application (without the use of IIS) using VS.NET 2005 and the latest beta version of the .Net 3.0 framework.
Microsoft Visual Studio 2005 Version 8.0.50727.42 (RTM.050727-4200); Microsoft .NET Framework Version 2.0.50727
A WCF (Windows Communication Foundation) Application Consists of a Service and Client.
This tutorial will show you how to create a simple WCF-to-WCF distributed application using two Windows Forms
applications. One will host the service, while the other will consume it--the client. Two projects will be created within
Visual Studio .Net 2005: 1) Windows Forms Service Service Host titled BasicServiceHost;
2) Windows Forms Service Client titled BasicServiceClient.
Startup VS.NET 2005 and create a new Windows Application Project titled BasicServiceHost.
Add to the BasicServiceHost project a Reference to the .Net 3.0 assembly System.ServiceModel (v3.0.0.0).
Add a Class file to the project and name it DotNetFunService.cs.
The DotNetFunService.cs class file should contain the following code:
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace BasicServiceHost
{
[ServiceContract]
interface IDotNetFunServiceContract
{
[OperationContract]
int AddNumbers(int Number1, int Number2);
}
public class DotNetFunService : IDotNetFunServiceContract
{
#region IDotNetFunServiceContract Members
public int AddNumbers(int Number1, int Number2)
{
return (Number1 + Number2);
}
#endregion
}
}
Via the Form1's Visual Designer, add three Buttons and a Label as shown below:
The Form1.cs class file should contain the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
namespace BasicServiceHost
{
public partial class Form1 : Form
{
private ServiceHost sh;
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
this.Start();
}
private void Start()
{
this.lblStatus.Text = "Starting...";
this.btnStop.Enabled = true;
this.btnStart.Enabled = false;
Application.DoEvents();
this.sh = new ServiceHost(typeof(DotNetFunService),
new Uri("http://localhost:8001/DotNetFunService"));
this.sh.Open();
this.lblStatus.Text = "Started";
}
private void btnStop_Click(object sender, EventArgs e)
{
this.Stop();
}
private void Stop()
{
this.lblStatus.Text = "Stopping...";
this.btnStart.Enabled = true;
this.btnStop.Enabled = false;
Application.DoEvents();
this.sh.Close();
this.lblStatus.Text = "Stopped";
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Stop();
Application.Exit();
}
}
}
The Start Service Host button should be named btnStart, the Stop Service Host button
should be named btnStop, the label in the center should be named lblStatus, and the Close
button should be named btnClose.
The Start Service Host button's Click event handler should be the
btnStart_Click method.
The Stop Service Host button's Click event handler should be the
btnStop_Click method.
The Close button's Click event handler should be the
btnClose_Click method.
Add an app.config XML application configuration file to the BasicServiceHost Project.
The app.config file should look like this:
Start the host application by 1) right-clicking on the DotNetFunService project, 2) navigating to
Debug, and then 3) clicking on Start new instance.
Now start listening for new requests by starting the host service in the application by clicking on the
Start Service Host button.
Creating a Basic WCF Service Client
Create a new Windows Application Project titled BasicServiceClient.
Add to the BasicServiceClient project a Reference to the .Net 3.0 assembly System.ServiceModel (v3.0.0.0).
Open a Visual Studio 2005 Command Prompt
(typically by navigating to Start/Programs/Microsoft Visual Studio 2005/Visual Studio Tools).
Make a note of what directory you are currently in, as this is from where you will be adding a client
proxy to the BasicServiceClient project (it should be something like
C:\Program Files\Microsoft Visual Studio 8\VC).
Generate a client proxy class by entering the following command:
svcutil /language:C# /config:app.config http://localhost:8001/DotNetFunService
The svcutil command executed in the preceding step above should have created two new files: 1)
DotNetFunService.cs (the proxy class file for the client) and 2) app.config
(the client's application configuration file).
Add the two files generated by svcutil to the BasicServiceClient. You may have to
change the Files of type setting when adding the files so that you can see both the
DotNetFunService.cs and app.config files.
Via the Form1's Visual Designer, add three Buttons and a Label as shown below:
The Form1.cs class file should contain the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BasicServiceClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAddNumbers_Click(object sender, EventArgs e)
{
DotNetFunServiceContractClient dnfc = new DotNetFunServiceContractClient();
this.lblAnswer.Text = dnfc.AddNumbers(100, 100).ToString();
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
The Call AddNumbers Method() button should be named btnAddNumbers, the label in the center
should be named lblAnswer, and the Close button should be named btnClose.
The Call AddNumbers Method() button's Click event handler should be the
btnAddNumbers_Click method and the Close button's Click event handler should be the
btnClose_Click method.
You can now test the client by starting a new Debug instance of it. Once the client is running, click on
the Call AddNumbers Method() button. Initially, the connection will be slow, but then, on subsequent
calls, it will be faster. After the call to AddNumbers is complete, the label lblStatus will
have a Text value of 200 (since AddNumbers has a Number1 of 100 and a Number2 of 100).