Hi,
Can we create customized binding in WCF? If yes, explain it?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hemant KumarPosted Sep 23, 2011, 1:36 AM
First, let's code our service. We'll use a slimmed version of the code generated by the "New / WCF Service Library" template in Visual Studio 2008.
Let me point out that there is absolutely nothing in our code that says where we are going (the address) or how we are going to get there (the binding). This is simply the codified version of what we are going to say to each other in our distributed conversation (the contract). In other words, we specify the data to be transmitted over the wire, but we never say how it will be transmitted (encoded as text, binary, JSON, or other) and where it will go (over HTTP, TCP, a queue, a carrier pigeon's foot, etc). That's what our configuration file achieves, and this is one of the things I love about WCF. The developers focus on what the service is supposed to achieve, and the operations / IT pro folks can focus on the infrastructure stuff.
Now take a look at how we can configure the service to use a custom binding. I err on the side of verbosity here, including the entire configuration file for easier understanding... this is how you configure the service.
Yeah, it's verbose, and my eyes are burning, too, just looking at all that XML. The part you should really focus on is the definition of "myBinding" which includes the textMessageEncoding and httpTransport elements. Notice that the textMessageEncoding element specifies the messageVersion attribute "Soap11". There are multiple possible values for the messageVerision attribute:
We are going to talk using basicHttpBinding which, by default, uses text encoding with SOAP 1.1 over HTTP.
Last, we need to configure the client. We can just use "Add Service Reference" in Visual Studio 2008, or use svcutil.exe and generate the client config ourselves. Or, even easier, we can write the client config ourselves, and it will be much more compact than what VS2008 or svcutil generates.
Nice and clean... we can simply use basicHttpBinding on the client because it does the exact same thing that our config on the service does... it talks SOAP 1.1 over HTTP using a text encoding. In fact, we could replace our service's config with the following and it would not change behavior at all (because our custom binding is equivalent to the basicHttpBinding).
Jaganathan BantheswaranPosted Sep 23, 2011, 1:35 AM
Yes we can.
Here MSDN explains for you