Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

Introduction

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

image001.png

Exposing BasicHttp Binding

Open the web.config file and check the Servicemodel element. You will not find any defined binding.

image002.png

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:

image003.png

Exposing wsHttp Binding

In order to introduce a wsHttp binding, make an entry in the web.config file as follows:

image004.png

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:

image005.png

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:

image006.png

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.

image007.png

Once you click on OK, the stub code will be generated. Open Form1.cs and add the following namespaces:

image008.png

Implement the following code in the form buttons in order to call the service using basic http binding:

image009.png

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:

image010.png

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:

image011.png

Implement the following code in the form buttons in order to call the service using web binding:

image012.png

Run the form applications and click on buttons. You will get values returned by the service.

image013.png image014.png

History

  • 18th December, 2011: Initial version

License

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

Share

About the Author

Kausik J. Nag
Architect
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberJPaula29-Dec-11 1:25 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 21 Dec 2011
Article Copyright 2011 by Kausik J. Nag
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid