Using and Gererating WCF Proxy class

Datetime:2016-08-23 04:33:23          Topic: WCF           Share

In this article I will discuss about the WCF proxy class. Basically this article will include what is a WCF proxy class and ways to generate it on the client. In my previous articles I have discussed about the WCF contracts and creating a WCF service from scratch I will use C# language for this article.

WCF Proxy Class

Below are the definition of the proxy class and important points about it.

WCF proxy is a layer between Client and service. To access the services and for hiding the complexities of the services from the client we put this class.

This is the layer that extract all the low level communication that is necessary to establish a connection with the service layer which is on another machine. It helps in hand shaking and taking care of security , serialization and de serialization. Client only needs to know about certain operations.

Important points about WCF proxy class

Consuming the service

  • .NET consumer access service by proxy class
  • WCF provides a base for the proxy class as proxy class is a wrapper for all the low level complexity.
  • Base class for proxy class is ClientBase<T> where we are getting all the implementation.
  • We have to provide the operations for the consumer to call.
  • T is the service contract which tell that there is one proxy class per service contract
  • Client does not know about service it only knows about service contract.
  • If service implements three contracts we need to have three service proxies for each contract.
  • Proxy class implements the service contract interface.
  • Service proxy mimics the methods which the service provides and it does this by implementing the contract methods which it inherits from service contract.
  • Proxy methods use “Channel” property to call the operations from ClientBase<T>.
  • Proxy requires endpoint information which can be fed through code or through configuration.
  • Proxy class project need not to have access to the service project as it is the front end and it only need to know about the contract project .

Ways To Create a Proxy Class

There are three ways by which we can generate a WCF proxy class for the client to consume. I will discuss them one by one.

  1. Inheriting a class from ClientBase<T> – As shown in one of my article here where I am creating the proxy by inheriting from the ClientBase<T> generic class.
  2. Using svcutil.exe – I will use the service from my previous article . You can download the   service project from here

    In this project I am self hosting the service in console application. To use the service from the svcutil.exe I should enable the metadata exchange in App.config of host project. The code for the change is as shown below figure.

    Fig: Enabling metadata Exchange

    Now to generate the proxy class we have to call the service from command prompt as shown in the below figure.

    Fig: Generating proxy using svutil.exe

  3. By Adding Service Reference –  We can add a service reference as shown in the below figure. Right click in the client project > Add > Service Reference.

    Fig: Adding WCF proxy using reference

Conclusion:

In this article I discussed about the WCF proxy class. I have also added ways by which we can generate proxy class in WCF.





About List