Proxy > Gmail Facebook Yahoo!

Creating a WCF Client




The fourth step involves creating the Windows Communication Foundation (WCF) client. It can be created using a Service Model Metadata Utility Tool (svcutil.exe) that can be found inside the Visual Studio SDK. This utility tool enables to extract the metadata associated to the WCF service and auto-generates a managed code for WCF service proxy in the specified programming language. It also generates a configuration file containing the information about WsDualHttpBindingand service endpoints which enable the client to communicate with the WCF Service.

Steps to Create a WCF Client

1. Add a new Console Application into the same WCFService solution by selecting its template from under the File menu -> New Project option.
2. In the Name box enter WCFService_Client as application name and press ok to create an empty console application.
3. After creating the console application, it will show the Program.cs file by default containing the following Main method code:
namespace WCFService_Client
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
4. Add a reference to System.ServiceModel.dll by right-clicking the project in solution explorer and choosing Add Reference option.
5. Now before proceeding further we need to create a proxy class using Service Model Utility Tool. Locate the tool inside the Microsoft SDK under the following path:
For x64 windows:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
For x86 windows:
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin
First run the WCFService application to self-host and run the created WCF service then open the command prompt and change directory to the above specified path and run the following command to generate the managed source code in C#:
svcutil.exe /language:cs /out:wcfServiceProxy.cs /config:app.config http://localhost:5000/WCFService/Sample
After executing the above comment it will generate two output files inside the located tool path in the command prompt where the svcutil.exe exists. It will generate wcfServiceProxy.cs and app.configfile there. The Service Model Metadata Utility Tool will generate the following code in wcfServiceProxy.cs file:
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4952
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://WCFService", ConfigurationName="IArea")]
public interface IArea
{
   
    [System.ServiceModel.OperationContractAttribute(Action="http://WCFService/IArea/AreaOfRectangle", ReplyAction="http://WCFService/IArea/AreaOfRectangleResponse")]
    double AreaOfRectangle(double length, double width);
   
    [System.ServiceModel.OperationContractAttribute(Action="http://WCFService/IArea/AreaOfCircle", ReplyAction="http://WCFService/IArea/AreaOfCircleResponse")]
    double AreaOfCircle(double radius);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface IAreaChannel : IArea, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class AreaClient : System.ServiceModel.ClientBase<IArea>, IArea
{
   
    public AreaClient()
    {
    }
   
    public AreaClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
    {
    }
   
    public AreaClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }
   
    public AreaClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }
   
    public AreaClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
    {
    }
   
    public double AreaOfRectangle(double length, double width)
    {
        return base.Channel.AreaOfRectangle(length, width);
    }
   
    public double AreaOfCircle(double radius)
    {
        return base.Channel.AreaOfCircle(radius);
    }
}



Responses

0 Respones to "Creating a WCF Client"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar