Click here to Skip to main content
13,550,352 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


29 bookmarked
Posted 21 Dec 2011
Licenced CPOL

Reusing WCF Service Across Several Applications using Different Bindings

, 21 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Reusing WCF service across several applications using different bindings


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 GetData().


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 wsHttp binding.

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 GET, POST, PUT, 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 IMyService with 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 wshttp binding:


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.

image013.png image014.png


  • 18th December, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kausik J. Nag
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThanks a lot (a 5 star article) Pin
shadowrad19-Apr-13 6:24
membershadowrad19-Apr-13 6:24 
GeneralNice article Pin
Balaji_rcs27-Jun-12 23:29
memberBalaji_rcs27-Jun-12 23:29 
GeneralMy vote of 5 Pin
JPaula29-Dec-11 1:25
memberJPaula29-Dec-11 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 21 Dec 2011
Article Copyright 2011 by Kausik J. Nag
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid