Thursday, April 30, 2009

Creating sample Windows communication Foundation application using .net

This article describes how to create and run a sample Windows Communication Foundation with .NET Framework 3.0 and above. This shows step-by-step in .net sample project to create a WCF Service and host the service in a console application.


To develop a WCF application, we need

  • WCF Service library which implements the Service using ServiceContract, OperationContract, DatatContract. This has the core logic of the service

  • Service Host using ServiceHost. The Service can be hosted in a Console application, Windows Service , IIS or Windows Process Activation Service (WAS)

  • Create a client and consume the service


Step by step to create sample WCF application and consume it

1. Open Visual studio and Create a WCF service Library project as shown.

[WCF1.JPG]

2. Once the project is created, visual studio by default creates ServiceContract with name IService1, OperationContract and DataContract.

[WCF2.JPG]

For sample purpose, we use the default contracts created by visual studio.

3. Implement the operation contract. This operation is used by the WCF clients to consume the service
public class Service1 : IService1 {

public string GetData(int value) {

// Todo Custom code here

return string.Format("Sample WCF application. you have Entered {0}", value);

}

}

4. Configure the endpoints,base address using app.config file as below

[WCF3.JPG]

5. create a new windows console application(WCFHostSample) to host the WCF Service (WcfServiceLibrarySample) as shown below.


[WCF4.JPG]

Add the reference to the WcfServiceLibrarySample library as shwon below.

[WCF5.JPG]

6. Include the following namesapce in WCF host sample and add the code in console application for hosting the service

using System.ServiceModel;

using System.ServiceModel.Description;

static void Main(string[] args)
{
// This is to host the WcfServiceLibrarySample
ServiceHost servicehost = new ServiceHost(typeof(WcfServiceLibrarySample.Service1));
servicehost.Open();
Console.WriteLine("Service started.....Press to terminate service.");
Console.ReadLine();
servicehost.Close();
}

Note : create an app.config for this host console application(WCFHostSample). Copy and paste the app.config (with end points and behavior nodes) created from service library to console application since the service library is loaded from this console application and uses the configuration of host console application.

Alternate way to host the WCF service without configuration file(app.config) and using ServiceMetadataBehavior as descibed below

static void Main(string[] args)
{
ServiceHost servicehost = new ServiceHost(typeof(WcfServiceLibrarySample.Service1));
//// A channel to describe the service.
ServiceMetadataBehavior metadataBehavior;
metadataBehavior = servicehost.Description.Behaviors.Find ServiceMetadataBehavior ();
if (metadataBehavior == null)
{
// This is how I create the proxy object add behavior to the service
metadataBehavior = new ServiceMetadataBehavior();

// configure url
metadataBehavior.HttpGetUrl = new Uri("
http://localhost:8731/WcfServiceLibrarySample/SampleApp");
metadataBehavior.HttpGetEnabled = true;

// add behviour to the host
servicehost.Description.Behaviors.Add(metadataBehavior);
}
servicehost.Open();
Console.WriteLine("Press to terminate service.");
Console.ReadLine();
servicehost.Close();
}

7. Start the WCF host console application as shown in the below. This should be running to consume the service from an client.

[WCF5a.JPG]

Note: Once the Service host is closed, then WCF service cannot be consumed from client


8. Create a windows console application (WCFClientSample) for consuming the WCF service hosted using the endpoint.


[WCF6.JPG]

9. Add Service reference using the url configured in the application configuration file and name the reference( as WCFSampleRef) as shown below.

[WCF7.JPG]

10. Then write the following code in WCF client project to consume the WCF service operations

static void Main(string[] args)
{
// create intstance of the WCF proxy
WCFSampleRef.Service1Client serviceproxy = new WCFClientSample.WCFSampleRef.Service1Client();
// CAll the Service GetData Method
Console.WriteLine(serviceproxy.GetData(100).ToString());
Console.ReadLine();
}

11. Then run the client and it get the following output

[WCF8.JPG]