Saturday, March 5, 2011

Introduction to WCF

This blog shows how to create a Visual Studio 2010 C# console application that hosts a Windows Communication Foundation (WCF) service exposing string manipulation methods.

The purpose of WCF is to simplify the communication of distributed applications on Windows. Because WCF uses RESTful communication and implements SOAP it can therefore operate with multiple platforms.

Creating the service

In Visual Studio 2010:
File > New > Console Application > Name: Service

Open the project’s properties and change the namespace to: Glyde.ServiceModel.Examples
Change the Program.cs namespace to use the Glyde.ServiceModel.Examples namespace.

Add System.ServiceModel.dll to the project.
Create a new public interface: IStringManipulator.cs, this will expose methods to add two strings together, replace part of a string and remove a substring.

Add a reference to System.ServiceModel in the interface.

Add a service contract to the interface:

[ServiceContract(Namespace = http://Glyde.ServiceModel.Examples)]

And add [OperationContract] to the exposed methods:

[OperationContract]
string Add(string s1, string s2);
[OperationContract]
string Delete(string s1, string s2);
[OperationContract]
string Replace(string s1, string s2, string s3);

Next we create a WCF service contract.

Create a StringManipulatorService class to implement the interface methods.

public string Add(string s1, string s2)
{
return s1 + s2;
}

public string Delete(string s1, string s2)
{
return s1.Replace(s2, string.Empty);
}

public string Replace(string s1, string s2, string s3)
{
return s1.Replace(s2, s3);
}

In the Program.cs create a URI object to serve as the base address – this will be used by the client to call the service:

Uri baseAddress = new Uri("http://localhost:8000/GlydeServiceModelExamples/Service");

Create a service host for the StringManipulatorService

ServiceHost selfHost = new ServiceHost(typeof(StringManipulatorService), baseAddress);

Add a service end point

selfHost.AddServiceEndpoint(
typeof(IStringManipulator),
new WSHttpBinding(),
"StringManipulatorService");

Enable the metadata exchange, for HTTP:

ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

Start the service
selfHost.Open();
Add a
Console.ReadLine();
…to keep the service running.
Call close to stop the service from running:
selfHost.Close();

Create a WCF client

Create a console application called Client, implement this in a separate application so the service and client can be run from a separate debug solutions – this will help your understanding and allow you to debug both.

Add System.ServiceModel.dll to the project and add the reference to Program.cs

How to use the service utility to create the client proxy

Switch back to the service solution and start the console application by F5

Start the Visual Studio command promptNavigate to the client projectRun the ServiceModel

Metadata Utility Tool to create the client code and configuration file (generated proxy):

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/GlydeServiceModelExamples/Service

This will generate the files in the client project folder:
generatedProxy
app.config

Add these to the client project by using ‘Add existing item’.

Remember, if you modify or add new methods in the service, the client proxy will have to be recreated.

The app.config specifies the location of the service, the binding and contract interface.
Call the service via the proxy

Create an endpoint address and a WCF instance client in the Program.cs.

StringManipulatorClient client = new StringManipulatorClient();

By adding the app.config and generatedProxy.cs the StringManipulatorClient is automatically available in the project.

Don’t forget to add System.ServiceModel to the project and add a reference in the Program.cs file.

Make a call to the client:

string value1 = "Hello Hannah, ";
string value2 = "how are you today";
string result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

value1 = result;
value2 = "Hannah";
string value3 = "Emma";
result = client.Replace(value1, value2, value3);
Console.WriteLine("Replace({0},{1}) = {2}", value1, value2, result);

value1 = result;value2 = ", how are you today";
result = client.Delete(value1, value2);
Console.WriteLine("Delete({0},{1}) = {2}", value1, value2, result);

Close the client after use:

client.Close();

So that’s it, I hope I have wept your appetite to learn more about WCF – As ever I have uploaded my source to GitHub click here to get the source from my introduction to WCF.

1 comment:

  1. Very nice post. Its really helpful for me. Here I have found another nice post across the internet on wcf introduction, You may check it by clicking on this link...
    http://mindstick.com/Blog/230/WCF%20Introduction

    Thanks Everyone!!

    ReplyDelete