Proxy Class For The WCF Service

In this article, I will demonstrate you how to create proxy class for the wcf service.

It requires creating Proxies or using ChannelFactory. Proxies create after hosting the service. 

There are two ways to communicate with client application in Windows Communication Foundation. First one is using the ChannedFactory and other one is creating proxies classes.

Today you will learn focus only for creating Proxy. Actually Proxy is a class in WCF that is used to communicate with client application. We can simply get the entire configuration through the proxy class. There is no need to do extra effort to generate the configuration setting for the client.

Proxy class used when you think that your service must be loosely coupled. If you make any changes in service then client must be rebuild.

There are the following details inside the proxy class.

  1. It stores the path of the service.
  2. Protocol which is using to communicate with client.
  3. It contains service implementation.
  4. It contains the signature for the service contract.

There are the following restrictions with Proxy Class.

  1. Proxy class does not work with read only or write only properties, It should be getter and setter.
  2. Proxy class expose only ServiceContract, we cannot expose any other contract or method which is not inside the Service Contract.
  3. Class Constructor does not expose by proxy class.

PROXY CLASS

There are different options to generate the proxy class for the WCF Service.

  1. By “Add Service Reference” from Visual Studio.
  2. Using SVCUtil.ext Utility.
  3. Implementing ClientBase<T> class.

Step 1: By “Add Service Reference” from Visual Studio

In Visual Studio, we can add the service reference inside the References. Be sure your service should be running before going to make reference of the service. So, once your service is running, we can add the service reference in client application.

Firstly, make sure your service is running. For this demo I have hosted my service and it is running.

Add Service Reference

To add the service reference into client application, Right click on References and choose Add Service Reference,

solution explorer

It will open a Add Service Reference window where you can pass your service URL to access the service. In my case, here's the service URL.

Add the service URL into Address section and click Go. It will show your service into Services section and all methods into Operation Section.

add service refrence

You need to provide the suitable namespace for the service reference and good to go. After clicking Ok, it will add the service reference with required file into your client project inside the Service References folder.

solution explorer

You can use the following way to call the service into client application.

client application

So, finally we have generated the proxy class from Visual Studio Add Service Reference Option.

Step 2: Using SVCUtil.ext Utility.

SVCUtil.exe is a tool for service utility. Using this you can also generate the proxy into client application for the service. Before going to create the proxy for the service, please make sure your service or host service is running.

Open Visual Studio Command Prompt where client application is going to run and generate the proxy class and configuration for the service. You can pass the proxy class name as /out:MathServiceProxy.cs.

cmd

It will generate two new files for us. One is our proxy class “MathServiceProxy.cs” and other one is “output.config”;

MathServiceProxy.cs

MathServiceProxy.cs

  1. //------------------------------------------------------------------------------  
  2. // <auto-generated>  
  3. // This code was generated by a tool.  
  4. // Runtime Version:4.0.30319.18408  
  5. //  
  6. // Changes to this file may cause incorrect behavior and will be lost if  
  7. // the code is regenerated.  
  8. // </auto-generated>  
  9. //------------------------------------------------------------------------------  
  10. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""4.0.0.0")]  
  11. [System.ServiceModel.ServiceContractAttribute(ConfigurationName = "IMathService")]  
  12. public interface IMathService   
  13. {  
  14.   
  15.     [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IMathService/Add", ReplyAction = "http://tempuri.org/IMathService/AddResponse")]  
  16.     int Add(int num1, int num2);  
  17.   
  18.     [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IMathService/Add", ReplyAction = "http://tempuri.org/IMathService/AddResponse")]  
  19.     System.Threading.Tasks.Task < int > AddAsync(int num1, int num2);  
  20.   
  21.     [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IMathService/Subtract", ReplyAction = "http://tempuri.org/IMathService/SubtractResponse")]  
  22.     int Subtract(int num1, int num2);  
  23.   
  24.     [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IMathService/Subtract", ReplyAction = "http://tempuri.org/IMathService/SubtractResponse")]  
  25.     System.Threading.Tasks.Task < int > SubtractAsync(int num1, int num2);  
  26.   
  27.     [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IMathService/Multiply", ReplyAction = "http://tempuri.org/IMathService/MultiplyResponse")]  
  28.     int Multiply(int num1, int num2);  
  29.   
  30.     [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IMathService/Multiply", ReplyAction = "http://tempuri.org/IMathService/MultiplyResponse")]  
  31.     System.Threading.Tasks.Task < int > MultiplyAsync(int num1, int num2);  
  32. }  
  33.   
  34. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""4.0.0.0")]  
  35. public interface IMathServiceChannel: IMathService, System.ServiceModel.IClientChannel {}  
  36.   
  37. [System.Diagnostics.DebuggerStepThroughAttribute()]  
  38. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel""4.0.0.0")]  
  39. public partial class MathServiceClient: System.ServiceModel.ClientBase < IMathService > , IMathService {  
  40.   
  41.     public MathServiceClient() {}  
  42.   
  43.     public MathServiceClient(string endpointConfigurationName):  
  44.         base(endpointConfigurationName) {}  
  45.   
  46.     public MathServiceClient(string endpointConfigurationName, string remoteAddress):  
  47.         base(endpointConfigurationName, remoteAddress) {}  
  48.   
  49.     public MathServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress):  
  50.         base(endpointConfigurationName, remoteAddress) {}  
  51.   
  52.     public MathServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress):  
  53.         base(binding, remoteAddress) {}  
  54.   
  55.     public int Add(int num1, int num2)   
  56.     {  
  57.         return base.Channel.Add(num1, num2);  
  58.     }  
  59.   
  60.     public System.Threading.Tasks.Task < int > AddAsync(int num1, int num2)   
  61.     {  
  62.         return base.Channel.AddAsync(num1, num2);  
  63.     }  
  64.   
  65.     public int Subtract(int num1, int num2)   
  66.     {  
  67.         return base.Channel.Subtract(num1, num2);  
  68.     }  
  69.   
  70.     public System.Threading.Tasks.Task < int > SubtractAsync(int num1, int num2)   
  71.     {  
  72.         return base.Channel.SubtractAsync(num1, num2);  
  73.     }  
  74.   
  75.     public int Multiply(int num1, int num2)   
  76.     {  
  77.         return base.Channel.Multiply(num1, num2);  
  78.     }  
  79.   
  80.     public System.Threading.Tasks.Task < int > MultiplyAsync(int num1, int num2)  
  81.     {  
  82.         return base.Channel.MultiplyAsync(num1, num2);  
  83.     }  
  84. }  
Output.config
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <configuration>  
  3.         <system.serviceModel>  
  4.             <bindings>  
  5.                 <basicHttpBinding>  
  6.                     <binding name="BasicHttpBinding_IMathService" />  
  7.                 </basicHttpBinding>  
  8.             </bindings>  
  9.             <client>  
  10.                 <endpoint address="http://localhost:8080/MathService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMathService" contract="IMathService" name="BasicHttpBinding_IMathService" />  
  11.             </client>  
  12.         </system.serviceModel>  
  13.     </configuration>  
Through the following way you can call this proxy class.

proxy class

So, finally we have created the proxy class using SVCUtil.exe.

Step 3:
Implementing ClientBase<T> class.

Firstly, create a class named with “MathServiceProxy.cs” and inherited ClientBase. You need to add System.ServiceModel namespace for this implementation.

MathServiceProxy.cs

Add the IMathService.cs implementation to use the service operation.
  1. using System.ServiceModel;  
  2. using MathServiceLibrary;  
  3.   
  4. namespace MathClient   
  5. {  
  6.     class MathServiceProxy: ClientBase < IMathService > , IMathService   
  7.     {  
  8.         public int Add(int num1, int num2)   
  9.         {  
  10.             return base.Channel.Add(num1, num2);  
  11.         }  
  12.   
  13.         public int Subtract(int num1, int num2)   
  14.         {  
  15.             return base.Channel.Subtract(num1, num2);  
  16.         }  
  17.   
  18.         public int Multiply(int num1, int num2)   
  19.         {  
  20.             return base.Channel.Multiply(num1, num2);  
  21.         }  
  22.   
  23.     }  
  24. }  
You can call this proxy class into your client application as  in the following way.

proxy class

So, finally we have achieved all three ways to generate the proxy class for the wcf service.

Thanks for reading this article, hope you enjoyed it.