Geeks With Blogs
Saqib Ullah BootStrapper Know How

One of the greatest features of WCF 3.5 is direct accessibility of WCF Service on ASP.Net page. Before that there is no direct way to call WCF 3.0 Service on ASP.Net page and you have to create a communication bridge in the form of Web service. Here you find on my blogs how to call AJAX-Enable WCF service from ASP.Net page.

Here is my interface called IoperationService and its implementation class OperationService.

   namespace AJAXEnableServices

            [ServiceContract(Namespace = "AJAXServices")]

            public interface IOperationService
     long Add(long x,int y);

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class OperationService : IOperationService
                        public long Add(long x,int y)
                                    return x + y;

Save the preceding code in "AJAXEnableService.cs" file.

The AspNetCompatibilityRequirements attribute apply on the WCF service to make it ASP.Net Compatible code. At runtime, application can detect if ASP.Net compatibility mode is enabled by checking the value of the static property AspNetCompatibilityEnabled. By default AspNetCompatibilityRequirements RequirementsMode is set on NotAllowed.

Create a file with .svc extension and add the following line of code that allow the WCF Service to call on HTTP.

<%@ServiceHost language=c# Debug="true" Service="AJAXEnableServices.OperationService" %>

Save it OperationService.svc.           

Now it a time to call our WCF service from client side, so we need to an Default.aspx page.

<html xmlns="" >
<head runat="server">
    <title>WCF Service Test Page.</title>
<script language="javascript" type="text/javascript">

            function callService()

                   var n1 = document.getElementById("num1").value;

                   var n2 = document.getElementById("num2").value;

                   var service = new AJAXEnableServices.OperationService();

                   service.Add(parseFloat(n1), parseFloat(n2), onSuccess, null, null);


            function onSuccess(result)

                   document.getElementById("result").value = result;



    <form id="form1" runat="server">
        AJAX-Enable WCF Service Client Page</h1>
        First Number:
        <input type="text" id="num1" /></p>
        Second Number:
        <input type="text" id="num2" /></p>   
        <input type="text" id="result" /></p>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            <asp:servicereference Path="OperationService.svc" />
        <input id="Button1" type="button" value="Price for 3 Sandwiches" onclick="return callService()" />

In the end call the Default.aspx page.

Posted on Thursday, May 29, 2008 8:21 PM .Net | Back to top

Comments on this post: Create AJAX-Enable WCF Service

# re: Create AJAX-Enable WCF Service
Requesting Gravatar...
It is nice but it doesn't solve my problem. Because in your case the Web Service/ WCF is included in the same project. But my problem is that i want to consume thisrd party WCF/Web Service or self made WCF/webservice which is not in same project.

So if you can help regarding this then please do.

Thank You
Left by Gulam Ali Ansari on Jun 11, 2008 12:14 AM

# re: Create AJAX-Enable WCF Service
Requesting Gravatar...
The approach you need to take is a bridge then; take a look at AJAX bridges, which require some extra work to call a third party. AJAX can't directly call an external web service.
Left by Brian on Jul 29, 2008 1:12 PM

# re: Create AJAX-Enable WCF Service
Requesting Gravatar...
What if SVC file created by visual studio? I can't edit it
Left by Moez Mousavi on Jan 16, 2010 9:18 PM

# re: Create AJAX-Enable WCF Service
Requesting Gravatar...
I am facing an issue with the JQUERY client side call to WCF service.
If WCF service is part of same project then it works absolutely fine, But If my WCF project is different then calling the WCF service is not working always sending me null or "". I am not able to trace where the issue is either in WCF service or calling. Please guide me on the same.
Left by Puneet Singhal on Jun 06, 2011 11:59 PM

# re: Create AJAX-Enable WCF Service
Requesting Gravatar...
I love to read this type of stuff. Good and attractive information I take from it..Thank you for posting such a nice article.
Left by Chanel Diaper Bags on Jul 21, 2011 8:34 PM

Your comment:
 (will show your gravatar)

Copyright © Saqib Ullah | Powered by: