What is WCF binding and Different Types of binding available.

What is WCF binding?
The binding in the config file tells you the bellow information
1. Binding element describing the transport protocol
2. Binding element that handles message encoding.

A binding should have one binding element, which should have the above 2 information.

BasicHttpBinding
This binding obeys the WS-I Basic Profile 1.1. This communicates using ASMX based web services.
WSHttpBinding
This binding follows the WS-* specifications. It supports distributed transactions, and secure, reliable sessions. This communicates using both HTTP and HTTPS transport Protocol. Messages are transmitted in the form of XML text or Message Transmission Optimization Mechanism (MTOM).
WSDualHttpBinding
Represents a binding that a WCF service can use to configure and expose endpoints that are able to communicate with ASMX-based Web services and clients and other services that conform to the WS-I Basic Profile 1.1
WSFederationBinding
This binding supports the WS-Federation specification. WS-Federation defines several models for providing federated security, based on the WS-Trust, WS-Security, and WS-Secure Conversation specifications.
NetTcpBinding
The name itself is saying that it uses the TCP protocol for communication. It uses binary encoding wile transfering messages. It offers higher performance than the bindings based on the HTTP protocols but less interoperability. It supports transactions, reliable sessions, and secure communications.
NetPeerTcpBinding
This binding supports peer-to-peer communications between applications using the TCP protocol. This binding supports secure communications and reliable, ordered delivery of messages. Messages are transmitted by using a binary encoding.
NetNamedPipeBinding
This binding is very helpful for processes running on a same computer. It gives high performance by using named pipes. This binding supports secure, reliable sessions and transactions. You cannot use this binding to connect to a service across a network.
NetMsmqBinding
The client and service can communicate using Microsoft Message Queue (MSMQ).Messages are stored in a message queue, so the client and the service do not both have to be running at the same time. This binding supports secure, reliable sessions and transactions. Messages use a binary encoding.
MsmqIntegrationBinding
You can build an WCF application, that sends or receives messages from an MSMQ message queue. It is intended for use with existing applications that use MSMQ message queues.

How to create a simple WCF service and consume it

Bellow i have explained how to create a simple WCF service in Visual Studio 2013
1. Click on new Project
New Project
2. From the left hand side of the templates please select WCF then you will get a list of templates associated with WCF.
select Template
3. Please select WCF service Application from the list. By default our visual studio will add one Interface and one svc file.
The interface will be have bellow contracts:

  1. ServiceContract
  2. OperationContract
  3. DataContract
  4. DataMember

it looks like bellow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}


The Service1.svc.cs file looks like bellow:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MyService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

4. The service will look like bellow in browser.

Service in browser
Now please create one website to consume the Service. Now we have the website.
The next work is to add service reference for the service, which we have created above.

1. Right click on the Website and the add service reference. Bellow i have provided the screenshots, please have a look.

Add Service reference

3. A pop up will open there you can add your service reference URL and you can add your service there.

Service reference window

You can get the service reference url by viewing the service in browser. From the browser you can get the service url.

Service Reference link
You can change your reference name like bellow:

Reference Name

Now you are done with reference adding in website. Now you can call the service for the website. Bellow here is some code

using MyServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class User : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1Client client = new Service1Client();
        lblUserName.Text = client.GetData(4);
    }
}

This was small demo to Create and consume WCF service. Please put your valuable comments.