Translate

Wednesday, August 10, 2011

How to consume a WCF service (WCF Client)

In this article I will show you how to consume (call) a WCF service. For  this example I will be consuming a service that was created in the  article http://dotnetanalysis.blogspot.com/2011/08/introduction-to-wcf-windows.html. Even if you haven't gone through this article, don't worry about it. You can consume any service in the same way as shown below.
Consuming a service involves basic steps

  • Adding a service reference
  • Creating an instance of the client
  • Calling the web methods of the instance

Follow the example below for details

1>Create a console application as shown below, call it ConsumeService

























2>Add a Service Reference to http://localhost:8000/DilbertService (note that this service should be running)



3>Double click on ServicReference1 on the right 

4>Locate the name of the client. You will use this name "DilbertFunctionsClient" in the next step.


5>Copy this into the program.cs code page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsumeService.ServiceReference1;

namespace ConsumeService
{
    class Program
    {
        static void Main(string[] args)
        {
            DilbertFunctionsClient client = new DilbertFunctionsClient();
            Console.WriteLine(client.Add(1,2));
            client.Close();
            Console.ReadLine();
        }
    }
}


6>At this stage this is how your application would look like























7>Run the app to see the output of client.Add(1,2)  



1 comment:

Comments will appear once they have been approved by the moderator