Looking at the WCF Demo

The heart of Service Oriented Architecture is the interaction between services. These used to all be “web” services that ran on web servers and traveled via http through ports 80 and 443. Recently, they’ve become more generic using tcp through any port and, so, they’re not web services any more, they’re just services.

Microsoft supports the interaction between services using the Windows Communication Framework (WCF) that connects services with clients and hosts. The demonstration of these components is pretty simple.

If we need information or we need someone to manipulate some information we already have, we’ll meet that need by identifying a provider. We’ll access the provider’s service. We’ll avail ourselves of the service’s solution by accessing it’s  endpoint. The endpoint defines three things:

  • The service location.
  • The binding – The information needed to interact with the service.
  • The contract – The definition of the functionality the service provides.

 The demos include:  

  1. Define a service and its contract.
  2. The service implementation.
  3. Configuring, hosting and running the service.
  4. Create a client proxy.
  5. Configure the client application.
  6. Create and run the client.

1. Define the Service, the Contract and Its Interface

Open Visual Studio 2010. Select File | New | Project.

Select a new console application. Give it a name and a location.

Right click on the Service project in Solution Explorer and select Properties. on the Application tab of the properties dialog change the default namespace to:

Microsoft.ServiceModel.Samples.

On the Program.cs page, make the same change to the namespace.

Rightclick on the project again and select Add Reference. On the .NET tab, select System.ServiceModel and click OK.

Add a using statement for System.ServiceModel in Program.cs.

Within the namespace, but after the Program class, add the service contract on our new namespace and its interface, ICalculator

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
  [OperationContract]
  double Add(double n1, double n2);
  [OperationContract]
  double Subtract(double n1, double n2);
  [OperationContract]
  double Multiply(double n1, double n2);
  [OperationContract]
  double Divide(double n1, double n2);
}

2. Create the Contract

After the ICalculator interface code, add a new class that implements the interface:

public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
   {
       double result = n1 + n2;
       Console.WriteLine("Received Add({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
   }
   public double Subtract(double n1, double n2)
   {
       double result = n1 - n2;
       Console.WriteLine("Received Subtract({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
   }
   public double Multiply(double n1, double n2)
  {
       double result = n1 * n2;
       Console.WriteLine("Received Multiply({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
  }
   public double Divide(double n1, double n2)
   {
       double result = n1 / n2;
       Console.WriteLine("Received Divide({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
   }
} 

3. Configuring, hosting and running the service.

Within the Main() method of the Program class, set the service’s base address:

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

 Then provide the hosting details, add the using statement:

using System.ServiceModel.Description;

Create the ServiceHost:

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

Then, add a try\catch block to add the endpoint:

try
{
   selfHost.AddServiceEndpoint(
       typeof(ICalculator),
       new WSHttpBinding(),
       "CalculatorService");

Enable the metadata exchange:

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

Create the open and close methods:

selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close(); 

 Close the try block and add the catch block to itself in the event of an error:

}
catch (CommunicationException ce)
{
    Console.WriteLine("An exception occurred: {0}", ce.Message);
    selfHost.Abort();
}

 At this point, you can run the service, either in Visual Studio or by clicking on the .exe in the project folder. Apparently there’s some goofiness with Windows Vista that requires you to do these as administrator but I’m not sure if a user in the local admin group will suffice. You’ll see the little black box pop up and then it will take an Enter keystroke to kill it.

You can also browse to the noted Uri and get a cool little “You have created a service.” page.

4. Create a client proxy.

 

 

References:

MSDN: Getting Started Tutorial

MSDN: Host a WCF Service in IIS

MSDN: Configuring Services Using Configuration Files

MSDN: ServiceModel Metadata Utility Tool (SvcUtil.exe)



Tags:

 
 
 

Comments are closed.