In this tutorial, I am going to explain you how to create .NET WCF Service with wsHttpBinding and webHttpBinding together in single WCF application. Below topics will be covered in this tutorial.
Creating new WCF Service application project
Creating simple calculator service
Creating complex / CompositeType calculator service
Configuring service with wsHttpBinding and webHttpBinding
Calling simple and complex service using .NET application by creating Proxy Class (wsHttpBinding)
Calling simple and complex service using REST client like POSTMAN (webHttpBinding)
Calling simple and complex service using jQuery (webHttpBinding)
Creating new WCF Service application project
I will be using visual studio 2013 and .NET framework 4.5.
Go to File → New → Project. A new window will be open as shown below.
Now go to Visual C# and search for WCF Service Application and give project name and click on OK .
Now a wcf service will be created with default template as shown below.
1) Now rename IService1.cs to ICalc.cs and Service1.svc to Calc.svc. 2) Now right click on Calc.svc, go to View Markup and change service from Service="WcfService1.Service1" to Service="WcfService1.Calc".
Creating simple and Complex calculator service
Now open ICalc.cs file, delete all the default code generated and replace its with new code as shown below. Here we have declared two method Calculator1 and Calculator2 inside inteface, both are decorated with POST verb and returns Json data. Calculator1 method accepts two integer parameter where as Calculator2 method accepts a complex class objects and both returns addition of two numbers.
// NOTE: You can use the "Rename" command on the
"Refactor" menu to change the interface name "IService1" in
both code and config file together.
web app root directory during debugging, set the value below to true.
Set to false
before deployment to avoid disclosing web app folder information.
Now run your application and you should see output shown below.
Access WCF service using .NET application by creating Proxy Class (wsHttpBinding)
Now create an ASP.NET Webform application or Console application or MVC application where you want to access or consume WCF Service. Let's create a console application.
Create a Proxy Class
We have created a console application. Now Right click on Refrences folder of console application and Click on Add Service Reference as below.
Now a new window will open as shown below. In address textbox, put URL of your WCF Service and click on Go button. Make sure your WCF Service application is running, in our case it is running on localhost. Give namespace name as MyServiceReference and finally click on OK.
Now visual studio will generate proxy class to communicate to WCF Service. Now open your app.config file of console application or web.config file in case of webform application, here you can see, console application will be using wsHttpBinding binding by-default.
Now we will access wcf service by using REST client like POSTMAN or you can use fiddler tool. You can install POSTMAN extension using google chrome browser.
Using Json DataType for Calculator1 method
1) Now open the POSTMAN, Select POST from dropdown. 2) Enter URL of WCF Service and append /web/method_name to end of the URL as we have given web for webHttpBinding and ws for wsHttpBinding in web.config of wcf service. 3) Go to Body tab, select raw radio button and select JSON(application/json) from dropdown. 4) Now put json data in body and click on SEND button as shown below. Response in json format can be seen second tab.
Using Json DataType for Calculator2 method
Repeat same process for Calculator2 method. You can see the output below.
Using XML DataType for Calculator1 method
Repeat same process as we did for json datatype. We need to select JSON(application/xml) from dropdown. To get xml input data format, go to WCF application, open Calc.svc.cs file and run application. Now WCF test client will get open. Click on Calculator1 method and go to XML tab copy XML code from body section as highlighted below.
Now go to POSTMAN and in body section paste the copied xml code and click on send button and response can be seen as below.
Using XML DataType for Calculator2 method
Repeat same process for Calculator2 method. Here XML generated using WCF test client is different from Calculator1 method as shown below.
Access WCF service using jQuery Ajax
Now we will access WCF Service using jQuery ajax method using webHttpBinding. But before access let's make some changes in WCF Service application. Go to WCF service application, add global.asax file if it not already added. Overwrite the Application_BeginRequest with below code.