Friday, January 29, 2010

Windows Communication Foundation

In this posting, let us share our experience of working with WCF (Windows Communication Foundation), an API that got introduced with .Net Framework 3.0. Earlier we used to work with various features like
  • Web Services 
  • .Net Remoting
  • Distributed Transactions and
  • Message Queues.

WCF, designed based upon SOA (Service Oriented Architecture) supporting distributed computing, address the above features as a single entity.

The major advantage of WCF is, it supports synchronous, asynchronous and REST style communications utilizing various protocols like http, netnamedpipebinding etc. We used netnamedpipebinding protocol for on-machine communication where the end point will be defined based on protocol type.

Various steps to create a WCF Service are:

Step 1: Create Interface Class and define the class with Service Contract attribute and methods with Operation Contract attribute.

namespace CalculatorService
{
    [ServiceContract(Namespace = "http://CalculatorService", SessionMode = SessionMode.Required)]
    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);
    }
}


Step 2: Create Service Class that implements the interface defined with Service Contract attribute and define the class with Service Behavior attribute. The Service Behavior attribute have two different parameters such as Instance Context mode and Concurrency mode. In Instance Context mode we have to specify the Service instance is single ton or it will be created for each call or each session. In Concurrency mode we have to specify whether it is single threaded or multi threaded.

namespace CalculatorService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class CalculatorService : ICalculator
    {
     
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }
}


Step 3: Host the Service as Console based application using the ServiceHost.

internal static ServiceHost myServiceHost = null;

internal static void StartService()
{
    try
    {
        myServiceHost = new ServiceHost(typeof(CalculatorService.CalculatorService));
        myServiceHost.Open();
    }
    catch (Exception ex)
    {
    }
}

internal static void StopService()
{
    try
    {
        if (myServiceHost.State != CommunicationState.Closed)
            myServiceHost.Close();
    }
    catch (Exception ex)
    {
    }
}



Step 4: Define the configuration in the host environment with protocol name and endpoint address.

  <system.serviceModel>
    <services>
      <service name="CalculatorService.CalculatorService" behaviorConfiguration="metadataSupport">
        <host>
          <baseAddresses>
           <add baseAddress="net.pipe://localhost/CalculatorService" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="netNamedPipeBinding" contract="CalculatorService.ICalculator" />
        <endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataSupport">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


Step 5: This step helps in generating the proxy object. Before generating the proxy, first run the service using command prompt. The following command will generate the service proxy object and configuration file.

Svcutil net.pipe://localhost/CalculatorService  /config:App.Config

The following will be the output

C:\Documents and Settings\WCFDeveloper >svcutil net.pipe://localhost/CalculatorService /config:App.Config
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.648]
Copyright (c) Microsoft Corporation.  All rights reserved.

Attempting to download metadata from 'net.pipe://localhost/CalculatorService ' using
WS-Metadata Exchange. This URL does not support DISCO.
Generating files...
C:\Documents and Settings\WCFDeveloper \CalculatorService.cs
C:\Documents and Settings\WCFDeveloper \App.Config

Client will connect to a service using a Proxy Object, which is connected to the specified endpoint of the service. Both the file need to be added in the client project.

Step 6: Calling the Service from client through the service proxy object.

Here CalculatorClient is the generated service proxy object.

CalculatorClient client= new CalculatorClient();;

private void uxAdd_Click(object sender, EventArgs e)
{
    Double num1 = Convert.ToDouble(uxNumber1.Text.Trim());
    Double num2 = Convert.ToDouble(uxNumber2.Text.Trim());
    uxResult.Text = client.Add(num1, num2).ToString();
}

            Now both the client and service are ready for deployment.

http://msdn.microsoft.com/en-us/library/ms751519.aspx          

1 comment:

data recovery services cost said...

I is absolutely useful it and I'll suggest to my friends. I know they will certainly be benefited from this blog.