Windows Communication Foundation - How to create a WCF Service contracts, Service, Service Proxy and consume a service from client application.

I was trying to write something about WCF service, How to create a service, service contract, service proxy and consume that service a client application.
The situation is I  am going to implement is , service will implement a GeometryService. This service will implement How to calculate Area. So from client user will send height and width to the service and service will calculate the area and send back the result to the client.

The architecture of this article's example will be as below,

Fig: Architecture diagram of the example.

So in this example I going to implement following components,

  1. GeometryContracts.dll (implemented as class library)
  2. GeometryServices.dll (implemented as class library)
  3. GeometryServicesHost (implemented as WCF application)
  4. GeometryServiceProxy.dll (implemented as class library)
and a test program named TestHarness.exe. 

GeometryContracts.dll will define all the contracts which will be used by the GeometryServices.dll. GeometryServices.dll will be host on IIS using GeometryServicesHost WCF application.  GeometryServicProxy.dll will be implemented using GeometryContracts.dll and ClientBase class of System.ServiceModel and will be used by Client application in this example TestHarness.exe application. So the following sequence diagram shows how the client application TestHarness.exe will use GeometryContracts.dll and GeometryServiceProxy.dll to consume the GeometryServices via IIS.




Fig: The sequence diagram, How client consume services via Proxy and Contracts.

Before we further just  have a quick look the structure of the solution,

GeometryContracts.dll class library will implement following types,

  • Data contract named Area.cs
  • Service contract ISurfaceArea.cs

GeometryServices.dll class library will implement following types as service which will implement the service definition ISurfaceArea.cs,

  • SurfaceAreaService.cs

GeometryServicesHost Wcf Application will have following files

  • SurfaceAreaService.svc
  • Web.config

GeometryServiceProxy.dll  will implmented following files,

  • SurfaceAreaServiceProxy.cs

TestHarness console application will be implemented  following files,

  • Program.cs
  • App.config

All the code is now I am going to include below,

GeometryContracts  created as class library project with following source code, when created this library I have add references following assembly,

System.Runtime.Serialization and
System.ServiceModel
Area.cs
namespace GeometryContracts
{
    using System.Runtime.Serialization;
    [DataContract]
    public class Area
    {
        [DataMember]
        public decimal Height { getset; }
        [DataMember]
        public decimal Width { getset; }
 
    }
}
ISurfaceArea.cs

namespace GeometryContracts
{
    using System.ServiceModel;
    [ServiceContract]
    public interface ISurfaceArea
    {
        [OperationContract]
        decimal CalculateArea(Area area);
    }
}


GeometryServices.dll created as a class library with following assembly references
System.ServiceModel and project references GeometryContracts the one I just created above. This library will have following source code,
SurfaceAreaService.cs
namespace GeometryServices
{
    using System;
    using GeometryContracts;
    public class SurfaceAreaService : ISurfaceArea
    {
        #region ISurfaceArea Members
 
        public decimal CalculateArea(Area area)
        {
            return area.Width * area.Height;
        }
        #endregion
    }
}



GeometryServicesHost Wcf application hosted on the local (the one comes with OS by default) IIS  and it will implement following source code,
SurfaceAreaService.svc


<%@ ServiceHost Language="C#" Debug="true" Service="GeometryServices.SurfaceAreaService" %>
There wont be any C# class file for the above file.
Web.Config
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="GeometryServices.SurfaceAreaService">
        <endpoint address="" binding="basicHttpBinding" contract="GeometryContracts.ISurfaceArea">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>



GeometryServiceProxy.dll will implemet the proxy for the client. This class library will have System.ServieModel as assembly reference and GeometryContracts.dll as project reference. This library will implement following files,


GeometryServiceProxy.cs
namespace GeometryServiceProxy
{
    using System.ServiceModel;
    using GeometryContracts;
 
    public class SurfaceAreaServiceProxy : ClientBase<ISurfaceArea>, ISurfaceArea
    {
        #region ISurfaceArea Members
        public decimal CalculateArea(Area area)
        {
            return base.Channel.CalculateArea(area);
        }
        #endregion
    }
}

So the service has been created and hosted on IIS. I will consume the service from the client application TestHarness.exe. This TestHarness is console application. This application will have GeometryContracts.dll and GeometryServiceProxy.dll as projects reference and with following source code,

Program.cs

namespace TestHarness
{
    using System;
    using GeometryServiceProxy;
    using GeometryContracts;
    class Program
    {
        static void Main(string[] args)
        {
            Area area = new Area();
            area.Height = 2;
            area.Width = 3;
            SurfaceAreaServiceProxy proxy = new SurfaceAreaServiceProxy();
            Console.WriteLine(proxy.CalculateArea(area));
        }
    }
}

and the App.config file with following contents
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <endpoint address="http://localhost/GeometryServicesHost/SurfaceAreaService.svc"
       binding="basicHttpBinding" bindingConfiguration=""
       contract="GeometryContracts.ISurfaceArea" name="basicEndPoint" />
    </client>
  </system.serviceModel>
</configuration>

I have completed the Service and Client side. When I run the application it will give output as below,

6.
This is just a simple application where I tried to show how to create and consume a WCF service. Happy to learn ideas from others.

Thanks
mohammad

No comments:

Post a Comment