Saturday, 10 August 2013

WCF - Create a sample WCF service application

To create a sample WCF service application, Do the following steps.

  1. Launch the Visual studio 2008 and above version.
  2. Select the New Project
  3. Select "WCF service application" in WCF template.
  4. Give the name of the Project as "CalcualtorService"
  5. click enter

Step 1 : Now select the Interface IService.cs and add an operation contract WelcomeMessage.

    public interface IService1

        string GetData(int value);

        string WelcomeMessage(string name);

        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here

Step 2 : Now select the service1.svc, Implement the method "WelcomeMessage" which will return the string type.

    public class Service1 : IService1
        public string GetData(int value)
            return string.Format("You entered: {0}", value);

        public string WelcomeMessage(string name)
            return string.Format("Welcome to the "+name);

        public CompositeType GetDataUsingDataContract(CompositeType composite)
            if (composite == null)
                throw new ArgumentNullException("composite");
            if (composite.BoolValue)
                composite.StringValue += "Suffix";
            return composite;

Repeat the above two steps for adding additional two methods Add and Sub. Add the following code in interface.

        int Add(int a, int b);

        int Sub(int a, int b);

Now add the following code of implementation in service1.svc

        public int Add(int a, int b)
            return a + b;

        public int Sub(int a, int b)
            return a - b;

    Now Press F5 WCF test client application will run to test the WCF service.In that application double click the method WelcomeMessage, Add . Sub which is present in left side of the panel in tree view. and give the input in the right panel in Request section.Press the Invoke button to see the output. It will be seen in response section.

Before hosting WCF Service Application configure the Web.Config by adding the multiple EndPoints and Behavior

      <service name="CalculatorService.Service1" behaviorConfiguration="CalculatorService.Service1Behavior">
        <endpoint address="" binding="basicHttpBinding" contract="CalculatorService.IService1">         
        <behavior name="CalculatorService.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />

Click the below links to see the host and invoke a WCF service through client 

Host a WCF Service

From this article you will find the how to create a WCF service application.