Monday, July 30, 2007

* WCF : Developing a basic WCF Client and Service

Previous Post <- Windows Communication Foundation (WCF) : An Overview

  • 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.ServiceModel 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.

Next Post ->> Tool : WCF Configuration Editor ( SvcConfigEditor.exe)

3 comments:

  1. Hi,
    I am quite new in WCF technology. I founded Your post very useful to me :) Thank You :). Only few small comments:
    1.When You are writing "Create a new console project." You could specify name of it. It is obvious that few lines later there is a namespace keyword with a Name that should be. But it would be much easier to do it if it would be typed together.
    2.On Windows Vista I have found few issues with svcutil and later with communication with a web service. Main problem was that app.config is generated with MyMachine:8080 address. In Vista are some problems with those (some security configurations) so I had to use my IP addess instead of "MyMachine".

    PS.Thank You for this simple and clear example.

    Regards
    Matthew

    ReplyDelete
  2. Thanks Matthew .. will take care.
    Vikas

    ReplyDelete
  3. Hi Vikas

    Nice to go thru you sample code. I too am a beginer to WCF. I didn't understand the piece of code svcutil /language:C# /config:app.config http://MyMachine:8080/WCFServices/MathService/mex.

    I hope this is to generate a proxy. Correct me if I am wrong. Can you please tell how to host the WCF application in IIS?

    Regards
    Wilson

    ReplyDelete