banner



How To Create Proxy For 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. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel" , "4.0.0.0" )]
  2. [System.ServiceModel.ServiceContractAttribute(ConfigurationName ="IMathService" )]
  3. public interface  IMathService
  4. {
  5.     [System.ServiceModel.OperationContractAttribute(Action ="http://tempuri.org/IMathService/Add" , ReplyAction = "http://tempuri.org/IMathService/AddResponse" )]
  6. int  Add( int  num1, int  num2);
  7.     [System.ServiceModel.OperationContractAttribute(Action ="http://tempuri.org/IMathService/Add" , ReplyAction = "http://tempuri.org/IMathService/AddResponse" )]
  8.     System.Threading.Tasks.Task <int  > AddAsync( int  num1, int  num2);
  9.     [System.ServiceModel.OperationContractAttribute(Action ="http://tempuri.org/IMathService/Subtract" , ReplyAction = "http://tempuri.org/IMathService/SubtractResponse" )]
  10. int  Subtract( int  num1, int  num2);
  11.     [System.ServiceModel.OperationContractAttribute(Action ="http://tempuri.org/IMathService/Subtract" , ReplyAction = "http://tempuri.org/IMathService/SubtractResponse" )]
  12.     System.Threading.Tasks.Task <int  > SubtractAsync( int  num1, int  num2);
  13.     [System.ServiceModel.OperationContractAttribute(Action ="http://tempuri.org/IMathService/Multiply" , ReplyAction = "http://tempuri.org/IMathService/MultiplyResponse" )]
  14. int  Multiply( int  num1, int  num2);
  15.     [System.ServiceModel.OperationContractAttribute(Action ="http://tempuri.org/IMathService/Multiply" , ReplyAction = "http://tempuri.org/IMathService/MultiplyResponse" )]
  16.     System.Threading.Tasks.Task <int  > MultiplyAsync( int  num1, int  num2);
  17. }
  18. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel" , "4.0.0.0" )]
  19. public interface  IMathServiceChannel: IMathService, System.ServiceModel.IClientChannel {}
  20. [System.Diagnostics.DebuggerStepThroughAttribute()]
  21. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel" , "4.0.0.0" )]
  22. public  partial class  MathServiceClient: System.ServiceModel.ClientBase < IMathService > , IMathService {
  23. public  MathServiceClient() {}
  24. public  MathServiceClient( string  endpointConfigurationName):
  25. base (endpointConfigurationName) {}
  26. public  MathServiceClient( string  endpointConfigurationName, string  remoteAddress):
  27. base (endpointConfigurationName, remoteAddress) {}
  28. public  MathServiceClient( string  endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress):
  29. base (endpointConfigurationName, remoteAddress) {}
  30. public  MathServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress):
  31. base (binding, remoteAddress) {}
  32. public int  Add( int  num1, int  num2)
  33.     {
  34. return base .Channel.Add(num1, num2);
  35.     }
  36. public  System.Threading.Tasks.Task < int  > AddAsync( int  num1, int  num2)
  37.     {
  38. return base .Channel.AddAsync(num1, num2);
  39.     }
  40. public int  Subtract( int  num1, int  num2)
  41.     {
  42. return base .Channel.Subtract(num1, num2);
  43.     }
  44. public  System.Threading.Tasks.Task < int  > SubtractAsync( int  num1, int  num2)
  45.     {
  46. return base .Channel.SubtractAsync(num1, num2);
  47.     }
  48. public int  Multiply( int  num1, int  num2)
  49.     {
  50. return base .Channel.Multiply(num1, num2);
  51.     }
  52. public  System.Threading.Tasks.Task < int  > MultiplyAsync( int  num1, int  num2)
  53.     {
  54. return base .Channel.MultiplyAsync(num1, num2);
  55.     }
  56. }

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. namespace  MathClient
  4. {
  5. class  MathServiceProxy: ClientBase < IMathService > , IMathService
  6.     {
  7. public int  Add( int  num1, int  num2)
  8.         {
  9. return base .Channel.Add(num1, num2);
  10.         }
  11. public int  Subtract( int  num1, int  num2)
  12.         {
  13. return base .Channel.Subtract(num1, num2);
  14.         }
  15. public int  Multiply( int  num1, int  num2)
  16.         {
  17. return base .Channel.Multiply(num1, num2);
  18.         }
  19.     }
  20. }

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.

How To Create Proxy For Wcf Service

Source: https://www.c-sharpcorner.com/UploadFile/8a67c0/proxy-class-for-the-wcf-service/

Posted by: ruddmyris1978.blogspot.com

0 Response to "How To Create Proxy For Wcf Service"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel