The Windows® Communication Foundation programming allows configuring services with a variety of wire formats and message protocols. The requirement of a different binding varies from application to application. The major advantage of different binding is, we can reuse the same the service methods for different consumers varying the bindings. We will see how we can achieve this.
Real Life Scenario
Let’s consider, some organization “ABC” has developed a WCF service. Now, some other organization “X” is developing a Silverlight application and it is interested in consuming the service. So, organization “ABC” configured the WCF service using
basicHttp binding in order to consume in a Silverlight application. After some days, some other organization “Y” is developing a ASP.NET web application and in it is interested in consuming the service using some WS(*) compliant binding. So, organization “ABC” introduces a
wsHttp binding in the WCF service, as per the requirement for consuming it in a more enhanced way in a ASP.NET application. Again some other organization “Z” is developing a JAVA based Web application and it is interested in consuming the service in REST way. They want the response representation from the service, not in typical XML format but in JSON format. Organization also fulfilled the requirement introducing a new
webHttp binding in order to consume the service in a REST way.
Let’s see how the above mentioned scenario can be addressed. For simplicity, I have consumed all three bindings in a single Windows Form client. But in real case, there might be different applications developed in Silverlight, ASP.NET or some JAVA based web client.
Implementing Different Bindings in the Same WCF Service
In order to implement the above discussed concept, we will develop a simple WCF service project. The WCF service has a single method
Exposing BasicHttp Binding
Open the web.config file and check the
Servicemodel element. You will not find any defined binding.
Actually, WCF exposes a
BasicHttp binding by default if there is no binding defined in the config file. Once you are interested in exposing multiple bindings you need exclusively define the
BasicHttp binding in the config file. As we will introduce several bindings in our WCF service, let's define a
BasicHttp binding for the service as shown below:
Exposing wsHttp Binding
In order to introduce a
wsHttp binding, make an entry in the web.config file as follows:
So, once we have exposed an
wsHttp binding for our service, our service is ready to serve any consumer who is interested in consuming the service using
Exposing webHttp Binding
In order to consume the WCF service in REST way, first we have to introduce some attribute in the interface definition of the service. In REST, we can pass parameter value of service methods using
querystring and generating
DELETE request. Here our service will be called using a HTTP
get request. Also our response representation will be in JSON format. Open the IMyService.cs file and mark the interface
WebGet attribute and
UriTemplate property as following:
The response format can be XML or JSON.
Now, we need to introduce the endpoint for consuming the service in REST way. For this, we have to define a endpoint behavior with
webHttp binding and then use the same as a endpoint behavior for the binding. To implement this, modify the web.config file as below:
Right click -> View in Browser on the file MyService.svc in order to check the WSDL generated by the WCF service.
Consuming the Service
Add a Windows Form application project and add three buttons on the form. Each button click will call the service using different binding. Right click on the References->Add Service Reference. Click on Discover or type the service URL in the Address box.
Once you click on OK, the stub code will be generated. Open Form1.cs and add the following namespaces:
Implement the following code in the form buttons in order to call the service using basic http binding:
The port number in the service URL should vary across machines. Get the service URL by Right click -> View in Browser on the file MyService.svc and check the port.
Implement the following code in the form buttons in order to call the service using
We can call the REST WCF service in several ways like using
get method in HTML forms or using
XMLHttpRequest. Here we are calling it using WCF proxy.
Add two more references as follows:
Implement the following code in the form buttons in order to call the service using web binding:
Run the form applications and click on buttons. You will get values returned by the service.
- 18th December, 2011: Initial version