WCF Consuming WCF Service - Windows Communication Foundation (WCF)

What is WCF Consuming WCF Service?

WCF services permits other applications to access or consume them. A WCF service can be frenzied 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 subsequent 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

wcf 5

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).

wcf 6

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

Step-4: Create a Proxy class.

The output appears as follows:

wcf7

Consuming Self-hosted WCF Service

Here, the entire process of consuming a self-hosted WCF Service is explained step-by-step along with ample coding and screenshots wherever necessary.

Step-1: Service is hosted, now we need to implement the proxy class for the client. There are different ways of creating the proxy.

  • Using SvcUtil.exe, we can create the proxy class and its configuration file with end-points.
  • Adding Service reference to the client application.
  • Implementing ClientBase<T> class

Of these three methods, Implementing ClientBase<T> is the best practice. If you are using the other two methods, we need to create a proxy class every time we make any changes in the Service implementation. But this is not the case for ClientBase<T>. It will create the proxy only at runtime and so it will take care of everything.

For this purpose, create one proxy class, which includes the references of System.ServiceModel and MyCalculatorService.

Now, create one console application, which includes the references of System.ServiceModel and MyCalculatorServiceProxy.

Step-2: End-point (same as service) information should be added to the configuration file of the client application.

Step-3: Before running the client application, you need to run the service. Shown below is the output of the client application.

wcf 8

Consuming WCF Service Hosted in WAS

Consuming a WCF service that is hosted in WAS is a simple process involving only a few steps. The steps are as follows:

  • Add the proxy class and the configuration file to the client application.
  • Create the object for the MathServiceClient and call the method.

The output appears as shown below.

wcf 9

Consuming WCF Service Hosted in Windows Service

The step-by-step process of how to consume a WCF service hosted in Windows Service is expressed below in detail with coding and instructions.

Once it is hosted successfully, we can create a proxy class for the service and start using in the client application. Here, it is shown with the IIS hosting type consuming.

wcf 10

Add the reference of ServiceModel.

The output appears as follows:

wcf 11

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Communication Foundation (WCF) Topics