WCF Articles

WCF Proxy Creation ways

WCF Proxy Creation ways

There are different ways of creating WCF proxy :

  1. Adding service reference to a WCF service using Visual Studio

  2. Use SvcUtil.exe utility to create proxy

  3. Implementing ClientBase<T> Class

Adding service reference to a WCF service using Visual Studio

Right click on project in IDE , you will see three Add options as shown in the figure below :

Click on Add Service Reference option , you’ll see the dialog box that is shown in Figure  :

Once you’ve clicked OK in the dialog box, the add-in spawns SvcUtil.exe, generating the necessary proxy class and the required configuration file (or modifying it) and adding the necessary references to the project.

This is the way to create sync WCF service proxy
To create WCF service proxy in async way please follow below steps before clicking on OK :
Click on Advanced Button

Click on Generate Asynchronous operation checkbox and then click on OK button

Using SVCUtil.exe
To generate manually, choose the CMD window by selecting Start | All Programs | Microsoft Windows SDK | CMD.The output files SVCUtil generates are the client proxy source code file and the application configuration file.
Command for producing both proxy class and configuration file
svcutil /config:app.config /out:”CalculatorService.cs” /language:csharp /n:*,SimpleClientWithProxy.CalculatorService “http://localhost/MathService/?CalculatorService.svc&#8221;
By default , svcutil generates only synchronous method signatures in proxy . 
Use option /async to generate both synchronous and asynchronous method signatures in proxy
Implementing ClientBase<T> Class
 Of all the methods , implementing ClientBase is best one because we need not create proxy class every time we make changes in service implementation.
public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
{
   
    public SampleServiceClient()
    {
    }
   
    public SampleServiceClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
    {
    }
   
    public SampleServiceClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }
   
    public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }
   
    public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
    {
    }
   
    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}

Categories: WCF Articles, WCF Tutorial

Leave a Reply