Home > .Net Technologies, C#/VB.Net, CodeProject, WCF > Learning WCF By Example

Learning WCF By Example

We will start our first WCF service example using a WCF Service Library template. Using this project type has some useful benefits. Firstly, it will automatically set references to required assemblies and generate some starter code to understand basics of the service. Secondly, it adds an App.config file in the project. Do not get surprised by this *.config file in a .Net *.dll! We will certainly reap benefit from this file when we will test our first WCF service.

Let’s follow these simple steps.
Open Visual Studio 2008. Add a project template as shown in the figure.

Add Temp
Fig 1: Visual Studio 2008 WCF Service Library project template

After adding, you will see two classes: IService1.cs and Service1.cs in the solution explorer. Rename these two files as IBasicMath.cs and MathService.cs respectively. Delete the starter code found in these files, and replace as follows.

In the first file,

namespace MathServiceLibrary
{
//
[ServiceContract]
public interface IBasicMath
{
//
[OperationContract]
int AddIntValues(int x, int y);

[OperationContract]
int SubtractIntValues(int x, int y);
}
}

In the second file,

namespace MathServiceLibrary
{
//
public class MathService : IBasicMath
{
//
#region IBasicMath Members
public int AddIntValues(int x, int y)
{
//
return x + y;
}
public int SubtractIntValues(int x, int y)
{
//
return x – y;
}
#endregion
}
}

Then, lets come to App.config file. We have to remove references of all IService1 and Service1 with IBasicMath and MathService respectively.

This way our first WCF service is ready. Now, let’s recall we have said we will reap benefit from App.config file in a library type project. We have mostly seen App.config in windows .Net *.exe only.

Visual Studio 2008 provides WcfTestClient.exe to test a WCF service. It is a GUI based application to test each member of service interface by reading settings values from App.config. This saves our time to manually build a host and client applications to test the service.

Once you run the application by pressing [F5], WcfTestClient will run.

TestClient
Fig 2: Testing the WCF service using WcfTestClient.exe

By clicking each method, we see the right view where we can supply input values and click “Invoke” button to get result. Also, note the message in the status bar of this window: “Service added successfully”.  If you do not see this status bar message when the WcfTestClient first loaded, chances are that we have made some mistakes in App.config.

I hope you got the result.
Let’s dig more into App.config file. Right click on it from the solution explorer and click Edit WCF Configuration menu item. Another GUI based WCF configuration editor opens. This is a SvcConfigEditor.exe. This utility is used to edit or create configuration files.

Svc
Fig 3: Exploring App.config settings using SvcConfigEditor.exe

I like to explain one more thing in the IBasicMath code. We see two attributes: ServiceContract and OperationContract. A WCF service is basically a collection of endpoints where each endpoint implements a service contract and contains a binding and an address. In short we call ABCs of WCF. A service contract is typically defined by applying attributes to a class or interface. It describes what the service does. OperationContract attribute is analogous to WebMethod attribute and marks the attached method as an exposed operation. See the class MathService, it has no such attribute and only IBasicMath has these attributes. In our current application, these ABCs can be seen in App.config <endpoint/> section.

I hope this tutorial helped us to understand the WCF service in a simple manner. In the next series, we will see how to host this service and test from a separate client application.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: