Binding in WCF – Part 11

In this article we learn what is binding and how we can implement binding in wcf.
Before reading this article, I highly recommend reading my previous parts:
KnownType Attribute in WCF: Part 6
MessageContract in WCF: Part 7
Tracing in WCF: Part 8
Exception handling in WCF: Part 9
Centralize Exception Handling in WCF: Part 10
Before start the example I am going to tell what binding is.
What Binding is
Binding is a mechanism by which communication details are specified to make connecting to a service endpoint. Each endpoint in WCF service requires a binding. Default binding is basicHttpBinding. Binding contain information about protocols, encoding and transport.
You can create your own custom binding as well. I will talk about this in later article. According to MSDN Most used common binding in WCF is
1. basicHttpBinding: This binding works only on http protocol. Best use of this binding is to converting web-services in WCF.
2. wsHttpBinding: This binding is basically does what basicHttpBinding does, but difference is this binding implements ws-* specification. It means wsHttpBinding = basicHttpBinding + security.
3. NetNamedPipeBinding: This binding provide cross process communication on the same machine. This binding does not work across machines.
4. NetMsmqBinding: This binding use MSMQ as transport. This is required when we have implemented some cross machine environment communication.
5. WebBinding: This binding works only on http protocol. It is used when we trying to implement restful services.

You can find more binding on msdn link. It depend on our project requirement which binding suites my project. On behalf of that we choose the bindings. You can also create our own custom binding. Create your own custom binding click here.

Lets make a wcf application. In this application we are trying to add and divide numbers using wcf services. I am using Visual Studio 2012. Open Visual Studio and go to File → New → Project → WCF and select WCF Service Application and name it wcfSevice

Visual Studio provides some auto generate code. Let’s delete IService1.cs and Service1.svc file.
Add a wcf service file and name it MathService.svc and write following code. It add two files..
IMathService.cs
MathService.svc
Delete all code of IMathService and write following code.

using System.ServiceModel;

namespace wcfService
{
[ServiceContract]
public interface IMathService
{
[OperationContract]
int AddTwoNo(int FirstNo, int Second);

[OperationContract]
int DivideTwoNo(int first, int second);
}
}

ServiceContract describe which operation the client can perform on the service.

OperationContract define within a ServiceContract. It define the parameters and return type of operation.
Now implement the IMathService interface in MathService.svc.cs. Let’s delete all code first from MathService.svc.cs and write the following

using System;
namespace wcfService
{
public class MathService : IMathService
{
public int AddTwoNo(int FirstNo, int Second)
{
return FirstNo + Second;
}

public int DivideTwoNo(int first, int second)
{
return first / second;
}
}
}

As earlier said default binding is basicHttpBinding. We can define binding in web.config file.
1
In this example there are two binding one is basicHttpBinding and another is netTcpBinding. All bindings have their own property.
I hope you will enjoy this article.
Happy coding.

Tags: , , , ,