Expose WCF Service to JavaScript in ASP.NET 3.5 C#


V4 Ajax Tutorials
Server Intellect Cloud Hosting

Expose WCF Service to JavaScript in ASP.NET 3.5 C#

This tutorial will show you how to create and implement an AJAX-enabled WCF Service into an ASP.NET Web page. WCF (or Windows Communication Foundation) is a union of technologies developed by Microsoft to make it easier and quicker for developers to build distributed applications.

We are going to create a single web page that will allow the input of two numbers from the user, then we will access a WCF Service to make a calculation and return the value back to the Web Page. We will do this using AJAX so that we will get a near-instant response.

To start, create a new Web Application in Visual Studio 2008, and then in Solution Explorer, right-click the Project, choose Add New Item.. AJAX-enabled WCF Service. Name it Service1.svc - you will see that it is added to the Solution Explorer, along with a Service1.cs in the App_Code folder. The .cs file should open upon creation, and look something like this:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
// Add [WebGet] attribute to use HTTP GET
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}

// Add more operations here and mark them with [OperationContract]
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

This is where we will add our logic to perform the calculation We will add a method to add two numbers together, like so:

[OperationContract]
public Double Add(Double theNum1, Double theNum2)
{
return theNum1 + theNum2;
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Notice we specify a static method, and the OperationContract attribute defines it as an operation of this Service.
Now if we move to our ASPX page, we will want to add a ScriptManager to the page to manage all of our AJAX calls behind the scenes. But in order for our Service method to be accessible at page level, we will need to add a Service reference to our WCF Service. We do so like this:

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service1.svc" />
</Services>
</asp:ScriptManager>

Now let's go ahead and create our form. We will use HTML form elements, as we are going to use JavaScript to call our Service method.

<input type="text" id="num1" size="3" /> + <input type="text" id="num2" size="3" /><br />
<input type="button" id="jsSubmit" value="Submit" onclick="AddNumbers()" />

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Nothing much different to a regular ASP.NET Web Application here. Notice we include the handler for the button. Here we are calling a JavaScript function, which we can now add to the page. Add the following to the bottom of the page:

<script language="javascript" type="text/javascript">
function AddNumbers() {
Service1.Add(document.getElementById('num1').value, document.getElementById('num2').value, onSuccess);
}

function onSuccess(string) {
alert('Answer = ' + string);
}
</script>

Here, we are directly referencing the public method we created within the WCF Service. We have access to this via JavaScript because we are using the ScriptManager to expose the Service to page level. We get the response from the method and display it in a JavaScript alert window. We have no need to write any code-behind logic, as all of the functionality is being handled by the Service. We can expand this Web Application by creating more methods (Operation Contracts) in the Service, and then referencing them in the same way via JavaScript.
Our entire Service code looks like this:

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
// Add [WebGet] attribute to use HTTP GET
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}

// Add more operations here and mark them with [OperationContract]
[OperationContract]
public Double Add(Double theNum1, Double theNum2)
{
return theNum1 + theNum2;
}
}

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Our ASPX page will look something like this:

<html>
..
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service1.svc" />
</Services>
</asp:ScriptManager>

<input type="text" id="num1" size="3" /> + <input type="text" id="num2" size="3" /><br />
<input type="button" id="jsSubmit" value="Submit" onclick="AddNumbers()" />
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
function AddNumbers() {
Service1.Add(document.getElementById('num1').value, document.getElementById('num2').value, onSuccess);
}

function onSuccess(string) {
alert('Answer = ' + string);
}
</script>

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
Grossesse Semaine Par Semaine said:

ASP.NET 2.0 and ASP.NET 1.1 offered a number of quality features too, but ASP.NET 3.5 has made use of such applications that has increased the popularity of ASP.NET as a development platform.

Posted Oct 28, 2010 8:46 PM
quebec incorporation said:

Web services such as Membership, Authentication, Profiles, Management, etc are also supported by ASP.NET 3.5 hosting. These are primarily ASP.NET application services.

Posted Oct 28, 2010 8:48 PM
quebec incorporation said:

ASP.NET 3.5 hosting also allows the web hosters to create such websites that enable AJAX for ASP.NET. The user is also given the opportunity to create such website that enable WCF and ASMX with the help of the Microsoft AJAX Library.

<a href="http://quebecincorporation.ca/">quebec incorporation</a>

Posted Oct 28, 2010 8:49 PM
forklift training said:

Service1.cs in the App_Code folder. The .cs file should open upon creation, and look something like this:

Posted Nov 16, 2010 2:18 AM
adam said:

holy cr@p the inline advertisements are annoying.

Posted Nov 17, 2010 1:22 AM
holiday clothes said:

It’s quite hard to find a good website. And I am very satisfied to have come here. The publications are doing great and full of good insights. I would be glad to keep on coming back here to check for a new update.

Posted Dec 1, 2010 10:26 AM
MLM Software said:

Notice we include the handler for the button. Here we are calling a JavaScript function, which we can now add to the page. Add the following to the bottom of the page:

Posted Dec 22, 2010 12:25 AM
Kenya Safari Tours said:

Now if we move to our ASPX page, we will want to add a ScriptManager to the page to manage all of our AJAX calls behind the scenes.

Posted Dec 22, 2010 6:50 AM
Lawyers in India said:

Now let's go ahead and create our form. We will use HTML form elements, as we are going to use JavaScript to call our Service method.

Posted Dec 24, 2010 5:20 AM
Best Deals Seattle said:

Now if we move to our ASPX page, we will want to add a ScriptManager to the page to manage all of our AJAX calls behind the scenes. But in order for our Service method to be accessible at page level,

Posted Dec 24, 2010 8:13 AM
Best Deals Seattle said:

Their setup is very easy and we were up and running in no time.

Posted Dec 24, 2010 8:14 AM
Videos Watch Online said:

I am very satisfied to have come here. The publications are doing great and full of good insights. I would be glad to keep on coming back here to check for a new update.

Posted Dec 28, 2010 10:36 PM
Funny Stories said:

Do you always write such good blogs. I am interested in reading more of your work. I have bookmarked your site. Thanks in advance.

Posted Jan 3, 2011 9:07 PM
Funny Stories said:

Do you always write such good blogs. I am interested in reading more of your work. I have bookmarked your site. Thanks in advance.

Posted Jan 3, 2011 9:07 PM
Funny Stories said:

Hey there! I could have sworn I’ve been to this blog before but after reading through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!

Posted Jan 3, 2011 9:09 PM
RadiologyTechnician said:

Notice we specify a static method, and the OperationContract attribute defines it as an operation of this Service.

Posted Jan 16, 2011 6:44 AM
ultrasound technicians said:

We get the response from the method and display it in a JavaScript alert window. We have no need to write any code-behind logic, as all of the functionality is being handled by the Service.

Posted Jan 16, 2011 7:45 AM
Bus to Kuala Lumpur said:

Very informative information of java script and ASP.NET.

Posted Feb 21, 2011 7:18 AM
viagra prix said:

Glad to come across this article

Posted Mar 9, 2011 5:15 PM
PDF Tools Review said:

Read The Best PDF Tools Review

Posted May 14, 2011 7:15 AM
Tassimo T Discs said:

I am very satisfied to have come here. The publications are doing great and full of good insights. I would be glad to keep on coming back here to check for a new update.

Posted Jun 2, 2011 7:09 AM
www.gucci2you.com said:

www.gucci2you.com

Posted Aug 6, 2011 3:39 AM
the energy egg said:

Great combinations, this what they've said that javascript is not very useful. I'll guess in php is not but with asp.net 3.0, very much useful.

Posted Jan 14, 2012 7:51 PM
Carpet Cleaning Hawthorne said:

Well done. I m certainly happy with the quality of the advice presented. I have high hopes that you keep up with the fantastic job accomplished.

Posted Jan 17, 2012 7:09 AM
T-Discs For Tassimo Coffeemakers said:

Trying to get a grip on AJAX for some time, this helped. Thanks!

Posted Jan 29, 2012 9:45 PM
android tracker said:

Best Ajax Tutorial I learn a lot from This

Posted Jan 30, 2012 1:34 AM

Leave a Comment