How to do it: Generate a proxy class through Visual Studio and consume an WCF Service

Code Samples
Table of Contents
Summary

Add service reference to a WCF service hosted on console application

Note

The procedure will be the same when referencing a WCF service hosted on a console application or IIS

Operations
File "Program.cs"
How to do it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCF
{
    class Program
    {
        static void Main(string[] args)
        {
            var _proxy = new wcfClient.ServiceReference.proxy.IwcfTestServiceClient("WSHttpBinding_IwcfTestService");
            var lvCategory = _proxy.getCategoryAsXML("1");
        }
    }
}
File "App.config"

All the configuration were added by Visual Studio through the reading of the web service metada, including all the configuref bindings

How to do it
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IwcfTestService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="65536">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Transport">
                        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                        <message clientCredentialType="Windows" />
                    </security>
                </binding>
            </netTcpBinding>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IwcfTestService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:55545/training/wcf/app/testservice1/soap"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IwcfTestService"
                contract="proxy.IwcfTestService" name="WSHttpBinding_IwcfTestService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:8000/training/wcf/app/testservice1/soap"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IwcfTestService"
                contract="proxy.IwcfTestService" name="NetTcpBinding_IwcfTestService">
                <identity>
                    <userPrincipalName value="Vegeta\João Seixas" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>
File "proxy.cs"
How to do it
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.269
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace wcfClient.ServiceReference.proxy {
    using System.Runtime.Serialization;
    using System;

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="Category", Namespace="http://schemas.datacontract.org/2004/07/WCF")]
    [System.SerializableAttribute()]
    public partial class Category : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

        [System.NonSerializedAttribute()]
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

        [System.Runtime.Serialization.OptionalFieldAttribute()]
        private string DescriptionField;

        [System.Runtime.Serialization.OptionalFieldAttribute()]
        private int IdField;

        [System.Runtime.Serialization.OptionalFieldAttribute()]
        private string NameField;

        [global::System.ComponentModel.BrowsableAttribute(false)]
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
            get {
                return this.extensionDataField;
            }
            set {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string Description {
            get {
                return this.DescriptionField;
            }
            set {
                if ((object.ReferenceEquals(this.DescriptionField, value) != true)) {
                    this.DescriptionField = value;
                    this.RaisePropertyChanged("Description");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public int Id {
            get {
                return this.IdField;
            }
            set {
                if ((this.IdField.Equals(value) != true)) {
                    this.IdField = value;
                    this.RaisePropertyChanged("Id");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string Name {
            get {
                return this.NameField;
            }
            set {
                if ((object.ReferenceEquals(this.NameField, value) != true)) {
                    this.NameField = value;
                    this.RaisePropertyChanged("Name");
                }
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="www.training.com", ConfigurationName="proxy.IwcfTestService")]
    public interface IwcfTestService {

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoryWithDataContractAsXML", ReplyAction="www.training.com/IwcfTestService/getCategoryWithDataContractAsXMLResponse")]
        wcfClient.ServiceReference.proxy.Category getCategoryWithDataContractAsXML(string pId);

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoryWithDataContractAsJSON", ReplyAction="www.training.com/IwcfTestService/getCategoryWithDataContractAsJSONResponse")]
        wcfClient.ServiceReference.proxy.Category getCategoryWithDataContractAsJSON(string pId);

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoriesWithDataContractAsXML", ReplyAction="www.training.com/IwcfTestService/getCategoriesWithDataContractAsXMLResponse")]
        wcfClient.ServiceReference.proxy.Category[] getCategoriesWithDataContractAsXML();

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoriesWithDataContractAsJSON", ReplyAction="www.training.com/IwcfTestService/getCategoriesWithDataContractAsJSONResponse")]
        wcfClient.ServiceReference.proxy.Category[] getCategoriesWithDataContractAsJSON();

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/addCategoryWithDataContractAsXML", ReplyAction="www.training.com/IwcfTestService/addCategoryWithDataContractAsXMLResponse")]
        int addCategoryWithDataContractAsXML(wcfClient.ServiceReference.proxy.Category pCategory);

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoryAsXML", ReplyAction="www.training.com/IwcfTestService/getCategoryAsXMLResponse")]
        string getCategoryAsXML(string pId);

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoryAsJSON", ReplyAction="www.training.com/IwcfTestService/getCategoryAsJSONResponse")]
        string getCategoryAsJSON(string pId);

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoriesAsXML", ReplyAction="www.training.com/IwcfTestService/getCategoriesAsXMLResponse")]
        string[] getCategoriesAsXML();

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/getCategoriesAsJSON", ReplyAction="www.training.com/IwcfTestService/getCategoriesAsJSONResponse")]
        string[] getCategoriesAsJSON();

        [System.ServiceModel.OperationContractAttribute(Action="www.training.com/IwcfTestService/addCategory", ReplyAction="www.training.com/IwcfTestService/addCategoryResponse")]
        int addCategory(string pName);
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface IwcfTestServiceChannel : wcfClient.ServiceReference.proxy.IwcfTestService, System.ServiceModel.IClientChannel {
    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class IwcfTestServiceClient : System.ServiceModel.ClientBase<wcfClient.ServiceReference.proxy.IwcfTestService>, wcfClient.ServiceReference.proxy.IwcfTestService {

        public IwcfTestServiceClient() {
        }

        public IwcfTestServiceClient(string endpointConfigurationName) :
                base(endpointConfigurationName) {
        }

        public IwcfTestServiceClient(string endpointConfigurationName, string remoteAddress) :
                base(endpointConfigurationName, remoteAddress) {
        }

        public IwcfTestServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
                base(endpointConfigurationName, remoteAddress) {
        }

        public IwcfTestServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
                base(binding, remoteAddress) {
        }

        public wcfClient.ServiceReference.proxy.Category getCategoryWithDataContractAsXML(string pId) {
            return base.Channel.getCategoryWithDataContractAsXML(pId);
        }

        public wcfClient.ServiceReference.proxy.Category getCategoryWithDataContractAsJSON(string pId) {
            return base.Channel.getCategoryWithDataContractAsJSON(pId);
        }

        public wcfClient.ServiceReference.proxy.Category[] getCategoriesWithDataContractAsXML() {
            return base.Channel.getCategoriesWithDataContractAsXML();
        }

        public wcfClient.ServiceReference.proxy.Category[] getCategoriesWithDataContractAsJSON() {
            return base.Channel.getCategoriesWithDataContractAsJSON();
        }

        public int addCategoryWithDataContractAsXML(wcfClient.ServiceReference.proxy.Category pCategory) {
            return base.Channel.addCategoryWithDataContractAsXML(pCategory);
        }

        public string getCategoryAsXML(string pId) {
            return base.Channel.getCategoryAsXML(pId);
        }

        public string getCategoryAsJSON(string pId) {
            return base.Channel.getCategoryAsJSON(pId);
        }

        public string[] getCategoriesAsXML() {
            return base.Channel.getCategoriesAsXML();
        }

        public string[] getCategoriesAsJSON() {
            return base.Channel.getCategoriesAsJSON();
        }

        public int addCategory(string pName) {
            return base.Channel.addCategory(pName);
        }
    }
}
References

The proxy client is generated through the “Add Service Reference” option. The valid URL of the web service  is required.

The WCF service to discover must be running and:

  1. if the mex endpoint is configured on the hosted service then the correspondent URL of then mex endpoint can be used;
  2. If the service metadata was enabled on the service behavior on the hosted service then the URL plus the query string  “?wsdl” can be used;
  3. if either (1) or (2) is configured then the service URL can be used

Add service reference to a WCF service