Loading

Windows Communication Foundation (WCF)

How to Consume WCF Service?. The Complete Windows Communication Foundation (WCF) Developer Course 2023 [Videos].

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

Wcf Consuming Services IIS 1

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 Consuming Services IIS 2

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 −

Wcf Consuming Services IIS 4

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.

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

namespace MyCalculatorServiceProxy {
   // WCF create proxy for ISimpleCalculator using ClientBase
   Public class MyCalculatorServiceProxy : 
   ClientBase<ISimpleCalculator>,
   
   ISimpleCalculator {
      Public int Add(int num1, int num2) {
         //Call base to do funtion
         returnbase.Channel.Add(num1, num2);
      }
   }
}

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

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

namespace MyCalculatorServiceClient {
   classProgram {
      Static void Main(string[] args) {
         MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy = newMyCalculatorServiceProxy.MyCalculatorServiceProxy();
         
         Console.WriteLine("Client is running at " + DateTime.Now.ToString());
         Console.WriteLine("Sum of two numbers. 5 + 5 =" + proxy.Add(5,5));
         Console.ReadLine();
      }
   }
}

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

<?xmlversion = "1.0"encoding = "utf-8" ?>
<configuration>
   <system.serviceModel>
      <client>
         <endpoint address 
            ="http://localhost:8090/MyCalculatorServiceProxy/ISimpleCalculator"
            binding = "wsHttpBinding" contract "MyCalculatorServiceProxy.ISimpleCalculator">
            </endpoint>
      </client>
   </system.serviceModel>
</configuration>

Step 3 âˆ’ Before running the client application, you need to run the service. Shown below is the output of the client application.

Wcf Consuming Services Self 3

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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespaceWASHostedClient {
   classProgram {
      staticvoid Main(string[] args) {
         MathServiceClient client = newMathServiceClient();
         Console.WriteLine("Sum of two number 5,6");
         Console.WriteLine(client.Add(5, 6));
         Console.ReadLine();
      }
   }
}

The output appears as shown below.

Wcf Consuming Services WAS 2

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 Consuming Services Windows Service 1

Add the reference of ServiceModel.

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

namespaceWindowServiceClient {
   classProgram {
      staticvoid Main(string[] args) {
         //Creating Proxy for the MyService
         MyServiceClient client = newMyServiceClient();
         Console.WriteLine("Client calling the service...");
         Console.WriteLine("Sum of two numbers 5,6");
         Console.WriteLine(client.Add(5, 6));
        
         Console.WriteLine("Subtraction of two numbers 6,5");
         Console.WriteLine(client.Sub(6, 5));
        
         Console.WriteLine("Multiplication of two numbers 6,5");
         Console.WriteLine(client.Mul(6, 5));
        
         Console.WriteLine("Division of two numbers 6,3");
         Console.WriteLine(client.Div(6, 3));
         Console.Read();
      }
   }
}

The output appears as follows −

Wcf Consuming Services Windows Service 3


See All

Comments (749 Comments)

Submit Your Comment

See All Posts

Related Posts

Windows Communication Foundation (WCF) / Blog

What is WCF?

WCF stands for Windows Communication Foundation. It is basically used to create a distributed and interoperable Application. WCF Applications came into the picture in .Net 3.0 Framework. This is a framework, which is used for creating Service oriented Applications. You can send the data asynchronously from one end point to another. I think you all know about Web Service and are thinking, if we already have Web Services and accessing on a remote basis then why did WCF come into picture?
22-Apr-2022 /50 /749

Windows Communication Foundation (WCF) / Blog

Major differences that exist between WCF and a Web service.

WCF (Windows Communication Foundation): WCF, as the name suggests, is a unified .NET framework that is used to develop service-oriented applications. It allows you to develop applications that can communicate using different communication mechanisms.
22-Apr-2022 /50 /749

Windows Communication Foundation (WCF) / Blog

Difference between WCF and Web API.

WCF is used to create a distributed and interoperable Applications. It provides a framework which is used for building service-oriented-connected applications for the transmission of the data as an asynchronous, from one service-point to other service-point. Previously known as Indigo and is a framework for building, configuring, and deploying network-distributed services.
22-Apr-2022 /50 /749