Monday, June 28, 2010

WCF : Developing a basic WCF Client and Service

 

  • Create a blank solution in VS 2005. To get better understanding of code we will not use WCF project templates installed with WCF sdk.
  • Add a class library
  • Add reference to System.Service Model assembly and namespace.
  • Create interface .. apply Attributes
namespace MathUtility
{
[ServiceContract]
public interface IMath
{
[OperationContract]
int AddNumbers(int x, int y);
[OperationContract]
int SubstractNumbers(int from, int number);
}
}


  • Create Service Class from interface. Implement Interface.

namespace MathUtility
{
public class MathService : IMath
{
public int AddNumbers(int x, int y)
{
return (x + y);
}

public int SubstractNumbers(int from , int number)
{
return (from - number);
}
}
}


  • Now we need to host this as a service. We will host in a console application.

  • Add a console application project. Add Application Configuration file to the project.

  • Add Reference to System.ServiceModel.

  • Modify the app.config file as following.

<configuration>
<!-- This Section is exclusive for WCF configuration for both service and client -->
<system.serviceModel>
<!-- This section is to declare services hosted using this cofig file -->
<services>
<!-- This section declares individual service. name=type name which implements service contract -->
<!-- behaviorConfiguration = name of bahavior config which we will define later -->
<service name="MathUtility.MathService" behaviorConfiguration="MathServiceBehavior">
<host>
<!-- base address of the hosted service to which clients will connect-->
<baseAddresses>
<add baseAddress="http://MyMachine:8080/WCFServices/MathService" />
</baseAddresses>
</host>
<!-- the actual address where service is exposed -->
<endpoint address="" binding="wsHttpBinding" contract="MathUtility.IMath"></endpoint>
<!-- The address where metadata is exposed and will be used by Svcutil.exe to generate client classes -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<!-- The behavior which we mentioned above in <service> section-->
<behavior name="MathServiceBehavior">
<!-- Exposing metadata on http. Not mandatory for fuctioning of service if client already has metadata-->
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>


  • Change host app as following.

namespace MathServiceHost
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceHost serviceHost = new ServiceHost(typeof(MathService));
serviceHost.Open();
Console.WriteLine("The Math Service is running");
Console.WriteLine("Press <ENTER> to stop the service.");
Console.ReadLine();
serviceHost.Close();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
}
}
}


  • Start the service.

  • Use Svcutil.exe to generate the client classes and app.config file.


>svcutil /language:C# /config:app.config http://MyMachine:8080/WCFServices/MathService/mex



  • Create a new console project. add generated .cs and app.config.

  • add reference to System.ServiceModel and give namespace to generated .cs file

  • You may have to remove identity tag from client config file if it has been generated. We will cover the reasons later.

  • Use the generated classes as follows :

namespace MathClient
{
class Program
{
static void Main(string[] args)
{
try
{
MathClient mathClient = new MathClient();
Console.WriteLine(mathClient.AddNumbers(1, 1));
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
}
}
}


  • Pls leave a comment to this post if you face any issue in executing the above tutorial.

0 comments:

Post a Comment