Saturday, June 7, 2008

Basic/Easy WCF application

Last time we saw how to do a simple or may be an unprofessional WCF application. In real-time scenarios, you would like to keep you service and host application separately so that host can load as many WCF applications or host remains unaffected by what happens to service. All boil down to Loosely coupled applications and SOA concepts

Let’s create a simple WCF application

Service
Note: If you do not have VS 2008, then create a simple Library, and add reference to System.ServiceModel namespace

HelloIndigoService.cs
namespace HelloIndigoService
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class HelloIndigoService : IHelloIndigoService
{
public string SayHello(string sText)
{
string st = String.Format("Received text {0} at {1}", sText, DateTime.Now);
return st;
}
}
}

IHelloIndigoService.cs
Create a new project and have the name as HelloIndigoService
Add the following code to it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace HelloIndigoService
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
[ServiceContract]
public interface IHelloIndigoService
{

[OperationContract]
string SayHello(string sText);
// TODO: Add your service operations here
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
/*[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}*/
}

Compile application

Host
We’ll see how to create a simple both Http and TCP host
Create a new Console Application project in the same solution. Name it HelloIndigoHost
Add Reference to HelloIndigoService.dll
Add Reference to System.ServiceModel

Add Application configuration file to the project and do the following.

Open App.Config using WCF Confgiuration tool



  1. Click on Create New Service and choose the dll created in the first project. The name you see should be like “HelloIndigoService.HelloIndigoService”

  2. Accept the default value for the Service Contract window

  3. In the communication mode window, select Http

  4. In the interoperability window, select “Advanced web service interoperablility” and then select “Simplex Communication”

  5. Provide a path of you choice. I chose a name like http://localhost:8000/IndigoService

  6. Click on Finish






Add another endpoint for mexHttpBinding. This is done to allow client query for WSDL information


  1. Expand “Services node” and click on the service you created (“HelloIndigoService.HelloIndigoService). Right-Click on End Point node, and select “New service endpoint”
  2. Name it as “HelloIndigoMexHttpEndoint”
  3. For Address set value as “mex”
  4. Set Binding as “mexHttpBinding”
  5. And contract as “IMetadataExchange”




Once you are done with the steps, then its time for us to construct an endpoint

  1. Name the end point as “HelloIndigoNetTcpEndPoint”
  2. Follow similar step for including a TCP Endpoint, just that address would be slightly different. I used the following net.tcp://localhost:7000/HelloIndigoService


In the configurations window, select the node “Advanced” and pick the node “Service Behaviors”. Add a new Service Behvior with the following configurations

  1. Give it a appropriate name. I called it “serviceBehavior”
  2. Click on the Add button in Configuration Behavior
  3. From the drop down window, select “Service Metadata”
  4. You would see that added to the Service behavior window
  5. Double click on “Service Metadata”. In the General tab set “HttpGetEnabled” property to TRUE
  6. Once you have created a Service Behavior, you need to associate it with the service
  7. Click on the service you created and set “Behvior configuration’ property to “serviceBehavior”





Once you are done with the settings, we have nothing left then to open and close the service host
Open Program.cs and stick the following code

static void Main(string[] args)
{
using(ServiceHost sHost = new ServiceHost(typeof(HelloIndigoService.HelloIndigoService)))
{
sHost.Open();
Console.WriteLine("Started service");
Console.ReadKey(true);
}
}

When ServiceHost is opened. Application looks for the endpoints in App.Config and opens as many as configured. In our case, it would be for http and tcp

You can test to see if these endpoints are working easily. If service host is executed from windows XP or above and the application executes without any error then, the end points are opened. HTTP endpoint can be verified though through IE. Open IE and give the path mentioned in the address section for http binding. In my case it was http://localhost:8000/IndigoService


Client
Now that we have constructed host…we are all set for client construction

If you execute the above link in IE, with mexHttpBinding, you’ll see a line which reads
Svcutil.exe http://localhost:8000?wsdl


  1. Copy that line
  2. Open Visual studio command prompt (You can find it from visual studio tools in start up menu)
  3. Execute the command
  4. This execution would yield to a couple of files. A proxy and config file.
  5. Create a new console application and name it “HelloIndigoClient”. Copy the files proxy and config file generated in the previous step
  6. Paste it in you client application.
  7. Rename the file to App.config

Add the following code

HelloIndigoService.HelloIndigoServiceClient hisc = new HelloIndigoClient.HelloIndigoService.HelloIndigoServiceClient("HelloIndigoNetTcpEndPoint");
Console.WriteLine(hisc.SayHello("Hello Indigo"));
Console.ReadKey(true);

With this the basic model for a professional webservice is ready. Now you can add DB connections, error handling and other logics to web service. This will have no impact on service host and client application. If there are any changes done to web service, generate proxy for web service and use the new files from client application.

Going forward, we'll create more webservice solutions to handle error conditions

No comments: