Sunday, May 25, 2008

WCF - Bare Bones

Creating a WCF application

This contains of 3 sections

  1. Service

  2. Service Host

  3. Client



Service
Application that will be consumed by clients.

A simple service will contain Data Contracts and Service Contracts

Service Contracts is the interface defined for communication. All exposed methods should be contained in this interface. There are no restrictions on the number of data contracts that will be exposed by a single WCF application.

Services necessarily need to communicate information, back to the caller. Return values can be basid data types (int, char, string, float…) or can be some typed objects (product, employee, order….). If the return value falls in the later category, then we should expose those types as DataMembers.

Enough of talking let me take you through a simple application that I created for use

Following class acts as a service contract and Data contract. If the application has a number of typed objects you may want to create separate files for each of the data contracts. I have kept things simple here

Let’s do a simple application to get familiar with WCF concepts. I’d build this application in such a way, that we complete a basic WCF implementation in just 10 mins time

Please note that this not a production standard way of doing it.. Take this like “Hello World application.

Create a new Console Application solution and name it SampleServiceHost
Add reference to System.ServiceModel namespace

This solution will contain both the service and service host. In real time, this should be split or held seperately. That’s when we achieve the basics of SOA.

A sample service would be the following. Write this code in the file program.cs with the scope of the default namespace. In our case it is SampleServiceHost

[ServiceContract]
public class MathOperations
{
[OperationContract]
public double GetSquare(double dNum)
{
return (dNum * dNum);
}
[OperationContract]
public double GetCube(double dNum)
{
return dNum * dNum * dNum;
}
}

The program that would invoke these line look like the following
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1234/SampleService");
using (ServiceHost sh = new ServiceHost(typeof(MathOperations), uri))
{
ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();
behaviour.HttpGetEnabled = true;
sh.AddServiceEndpoint("SampleServiceHost.MathOperations", new BasicHttpBinding(), uri);
sh.Description.Behaviors.Add(behaviour);
sh.Open();
Console.WriteLine("Service started");
Console.ReadKey(true);
sh.Close();
}
}
}

Let me take a little time to explain what’s happening in the above code

  1. Uri – holds the location to host your webservice. From this solution perspective, it has to be an available http location with port number that’s avialable for use. In the coming days we’ll see how to use https, tcp and netbinding url’s

  2. ServiceHost – As the name says, this is the class that hosts our library/classes for usage. This is done through construction of channel stack. I’ll explain more in my next solution

  3. ServiceMetadataBehavior is needed for us to pick wsdl from the service exposed. For now set the property HttpGetEnable = true for this class

  4. ServiceEndPoint – This is the class that’s used by Channel class to construct our service. I’ll talk more on this in my next title. This class takes the type of object that’s being exposed, type of binding, and class name with namespace

  5. Once all this is done, we have to open the servicehost, so that the client can communicate with the service in the URL



Note
In real time scenario, we should have the configuration of ServicendPoint and Behaviors in App.config file. That’s when we could call it a SOA implementation

So we are done with the service and service host implementation. Let’s see the service working. Open IE and copy past the URI path. In our case type in http://localhost:1234/SampleService

Output should be something similar to this


Client-Side

Open VisualStudio command prompt and copy the like
svcutil.exe http://localhost:1234/SampleService?wsdl

This would create 2 new files in the location you executed the above command
MathOperations.cs
Output.config


  1. Create a new Console application in the same solution with the name “SampleServiceClient”

  2. Copy the above 2 files into the project

  3. Add reference to System.ServiceModel namespace



Open Program.cs do the following
Create an instance of MathOperationClient class
invoke GetSquare and GetCube functions
Close client

Sample code for the client is appended
namespace SampleServiceClient
{
class Program
{
static void Main(string[] args)
{
MathOperationsClient client = new MathOperationsClient();
Console.WriteLine("Doing square of 10");
Console.WriteLine(client.GetSquare(10));
Console.WriteLine("Doing cube of 10");
Console.WriteLine(client.GetCube(10));
Console.ReadKey(true);
}
}
}

Code for the client is also complete. All that remains is execution of the application


  1. Open SampleServiceHost and execute it

  2. Open SampleSerciceClient and execute it



Enjoy WCF programming. I’ll update a professional sample to get goin on the next level of WCF shortly. Until then ……..bye