This tutorial shows you how to easily create and consume a WCF service into an AJAX Web Application using ASP.NET and C#

In this tutorial, we will show you an extremely easy way to implement WCF into a Web Page using AJAX. We use Visual Studio 2008 and ASP.NET 3.5 for this demonstration. The Service will allow us to submit a °F value to it, and it will convert that to °C and return that value back to the Web Page.

To begin, we will create a regular ASP.NET Web Application in Visual Studio, then we will right-click the Project in Solution Explorer and choose to Add New Item.. AJAX-enabled WCF Service. Name it FtoC, and wrap the class in a namespace, like so:

We can keep the same structure, and input our own method:

We declare the class as a ServiceContract and the method as an OperationContract, exposing it to the outside world.

Next, to use the Service in our ASPX page, we will make a reference to it:

Then we declare a ScriptManager on the page to handle our AJAX calls and also an UpdatePanel:

Next, we can add our ASP.NET controls to the page. We will add a textbox, button and literal:

Finally, the last piece of our application is to add the logic to our code-behind, which is a very simple call to the WCF Service, like any other:

Now when we run the page, we can instantly get the conversion of Fahrenheit into Celsius.
Download Source Files