WCF - Consuming WCF Service

WCF services allow other applications to access or consume them. A WCF service can be consumed by many ways depending on the hosting type. Here, we are explaining the step-by-step method to consume a WCF service for each of the following popular hosting options:

  • Consuming WCF Service hosted in IIS 5/6
  • Consuming WCF Service that is self-hosted
  • Consuming WCF Service hosted in Windows Activation Service
  • Consuming WCF Service hosted in Windows Service

Consuming WCF Service Hosted in IIS 5/6

The process of consumption of a WCF service hosted in IIS 5/6 is discussed below in detail. In addition, the discussion includes how to create proxy and console applications.

Step-1: Once a service is hosted in IIS, we have to consume it in client applications. Before creating the client application, we need to create a proxy for the service.

This proxy is used by the client application to interact with the service. To create a proxy, run Visual Studio 2008 command prompt.

Using service utility, we can create the proxy class and its configuration information.

svcutilhttp://localhost/IISHostedService/Service.svc

 

After executing this command, we will get two files generated in the default location.

  • MyService.cs – Proxy class for the WCF service

  • output.config – Configuration information about the service

Step-2: Now, we will start creating the Console application using Visual Studio 2008 (Client application).

 

Step-3: Add the reference 'System.ServiceModel'; this is the core dll for WCF.

Step-4: Create a Proxy class.

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

namespace MyServiceClient
{
   Class Program
   {
      Static void Main(string[] args)
      {
         // Creating Proxy for the MyService
         ServiceClient Client = newServiceClient();
         Console.WriteLine("Client calling the service...");
         Console.WriteLine("Hello Ram");
         Console.Read();
      }
   }
}

The output appears as follows:

你可能感兴趣的:(service)