Consuming WCF Service Using JavaScript


I was recently working on a project in which I needed to call the WCF Service using JavaScript in an ASP.NET web application. So, I created a sample application in which we'll see how to access the service using JavaScript.


In this article, we'll use the following sections:

  • Creating and Working with a WCF Service
  • Creating an ASP.NET Web Application
  • Consuming the Service
  • Running the application

So, let's begin the procedure in which I need to insert the data to the database table and for that we'll create the service and consume it to the another application.

Creating and Working with WCF Service

In this section we'll create the WCF Service Library with the following procedure.

Step 1: Open Visual Studio and create the WCF Service Library named DataServices.

Creating WCF Service Library

Step 2: Now we'll create the operation contract in the interface as in the following:

  1. using System.ServiceModel;  
  3. namespace DataServices  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IDataService  
  7.     {  
  8.         [OperationContract]  
  9.         string InsertData(string Name, string Email, string Message);  
  10.     }  
  11. }  

In the code above, the string type of InsertData() is created with three parameters.

Step 3: Now we'll create the class with the following code:

  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  5. namespace DataServices  
  6. {  
  7.     public class DataService : IDataService  
  8.     {  
  9.         public string InsertData(string Name, String Email, string Message)  
  10.         {  
  11.             SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog="DataBase Name";User ID="User Name";Password="Password"");  
  12.             SqlCommand cmd = new SqlCommand("SP_InsertProc", con);  
  13.             cmd.CommandType = CommandType.StoredProcedure;  
  15.             cmd.Parameters.AddWithValue("@name", Name);  
  16.             cmd.Parameters.AddWithValue("@email", Email);  
  17.             cmd.Parameters.AddWithValue("@message", Message);  
  18.             con.Open();  
  19.             cmd.ExecuteNonQuery();  
  20.             con.Close();  
  21.             return "Success";  
  22.         }  
  23.     }  
  24. }  

 In the code above, I've simply created the function with which we can insert the record into the database.

Step 4: Open your App.Config file and modify the code from the highlighted code below: 

  1. <service name="DataServices.DataService" behaviorConfiguration="DataServiceBehavior">  

 <endpoint address="" binding="basicHttpBinding" contract="DataServices.IDataService">  


 <behavior name="DataServiceBehavior">  

There is no need to change the rest of the text. The rest is configured automatically.

Creating ASP.NET Web Application

Now we'll add another project and in which we'll create an ASP.NET Web Application. So . let's begin with the following procedure.

Step 1: Right-click on the Solution and add a new project.

Adding New Project

Step 2: Select an ASP.NET Web Application and enter the name as WcfDataApplication.

Creating ASP.NET Web Application

Step 3: Select the Empty Project Template from the One ASP.NET Wizard.

Selecting Empty Project Template

Step 4: Now add a text file in the application and save it as "WcfDataService.svc".

Adding Text File

It is good to add the Ajax-enabled WCF Service, but it creates its own operation contract. So, I want to resemble the situation in which I can re-use an existing operation contract implemented in a separate service library.

Step 5: Copy the following code into the svc file: 

  1. <%@ServiceHost language="C#" debug="true"  
  2. Service="DataServices.DataService"  
  3. Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"%>  

Step 6: Now select the Service and click on the Show all files icon and copy all files from the Debug folder as shown below:

Debug Folder in Application

Step 7: Paste all files into the application Debug folder.

Step 8: Right-click on the WcfDataService.svc file and show it in the browser.

Ruunig WCF service

Step 9: Now just change the URL as http://localhost:1234/WcfDataService.svc/jsdebug. This will show the JavaScript as in the following:

Running Js file on brosser

Note: Please note that if you open this in Firefox, it'll display directly as shown in the preceding screenshot. Internet Explorer does not show it for security reasons. It'll ask you to save it.

Step 10: Now add a Web Form to the application and add the following code:

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.         <div>  
  4.             <table style="height: 170px; width: 295px">  
  5.                 <tr>  
  6.                     <td>Name:</td>  
  7.                     <td>  
  8.                         <input type="text" id="TxtName" /></td>  
  9.                 </tr>  
  10.                 <tr>  
  11.                     <td>Email:</td>  
  12.                     <td>  
  13.                         <input type="text" id="TxtEmail" /></td>  
  14.                 </tr>  
  15.                 <tr>  
  16.                     <td>Message:</td>  
  17.                     <td>  
  18.                         <input type="text" id="TxtMessage" /></td>  
  19.                 </tr>  
  20.                 <tr>  
  21.                     <td>  
  22.                         <input type="button" id="BtnSendDetails" value="Submit" onclick="CallDataService()" /></td>  
  23.                     <td>  
  24.                         <label id="LblMessage"></label>  
  25.                     </td>  
  26.                 </tr>  
  27.             </table>  
  28.             <asp:ScriptManager ID="ScriptManager1" runat="server">  
  29.                 <Services>  
  30.                     <asp:ServiceReference Path="~/WcfDataService.svc" />  
  31.                 </Services>  
  32.             </asp:ScriptManager>  
  33.         </div>  
  34.     </form>  
  35. </body>  

 In the code above, I've added the content in a table and added a ScriptManager. In the ScriptManager, the path of the Service Reference is the WxfDataService.svc file.

Consuming the WCF Service

In this section, we'll create the scripting code to consume the service in the web form page. Proceed with the following structure:

Step 1: Open the Service.svc file in the browser and copy the object name as in the following:

JavaScript File on Browser

Using this object you can do something to the function of the service.

Step 2: In the web form, edit the head section with the following code:

  1. <head runat="server">  
  2.     <title>Data Application</title>  
  3.     <script type="text/javascript">  
  4.         function CallDataService() {  
  5.             var UserName = document.getElementById("TxtName").value;  
  6.             var UserEmail = document.getElementById("TxtEmail").value;  
  7.             var UserMessage = document.getElementById("TxtMessage").value;  
  8.   , UserEmail, UserMessage, ShowMessage, null, null);  
  9.         }  
  11.         function ShowMessage() {  
  12.             LblMessage.innerHTML = "Data Inserted Successfully";              
  13.         }  
  14.     </script>  
  15. </head>  

 In the code above, there are two methods defined. The first method accepts all the values from the control and the values are passed to the service method to further process and call the next method. The next method just displays the text.

Run the Application

Now we'll run the web form. Enter the values.

Accessing Fom in Application

After clicking on the Submit button, the data is inserted to the database as in the following:

Accessing Service in Application


This article described how to create the WCF Service and access the service in the web application using JavaScript. Thanks for reading. Happy Coding.

Up Next
    Ebook Download
    View all
    View all