WCF Service using ASP.NET and Client Script

In this article, we will see how to develop a WCF Service using ASP.NET and Client Script.

Updated 8/29/2018 - Formatted 
 
Step 1 - Create an ASP.NET 3.5 website.
 
WcfService1.gif 
 
Step 2 - Right click the project > Add New Item > Choose 'AJAX-enabled WCF Service' from the Templates > name the service as 'GreetingService' > Add.
 
pic2.gif 
 
Step 3 - Open the GreetingService.cs to implement operations to be exposed in the service. Specify the Namespace for ServiceContractAttribute as 'GreetNM'.
 
WcfService3.gif 
 
Step 4 - Add a new method called GreetUser(string uname) which takes a username and returns a string with the "Hello" greeting appended to the user name.
 
WcfService4.gif 
 
Step 5 - Now open Default.aspx. Go the Design mode and drag and drop a <Script Manager> from the AJAX Extensions tab in the toolbox. With the Script Manager selected, open the 'Services' collection in the Properties window. The 'Service Reference Collection Editor' window opens up. Click Add and in the Path specify 'GreetingService.svc' > OK.
 
WcfService5.gif 
 
Step 6 - Now add a HTML Button, an HTML Input box and a <div> to the Default.aspx page. After renaming the controls and adding the onClick event of the button, the markup will appear similar to the following.
 
WcfService6.gif 
 
Step 7 - Call the service. You will notice how the intelligence brings up the GreetNM namespace. If you remember, we added a namespace to the service called 'GreetNM'.
 
[ServiceContract(Namespace = "GreetNM")]
 
Hence when the client-side script proxy is created, it is now in the GreetNM namespace.
 
Add code to call the WCF Service using client side script as shown below.
 
WcfService7.gif 
 
Step 8 - Well that was a simple introduction to AJAX-enabled WCF Service. An even simpler way to create script services is to use
 
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" in your .svc file.
  1. <%@ ServiceHost Language="C#" Debug="true" Service="AjaxWCF.GreetingService" CodeBehind="~/GreetingService.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>   
This Factory, automatically adds an ASP.NET AJAX endpoint to a service, without you needing to add the configuration details in the web.config file.
 
Step 9 - Resultant Window is as shown below.
 
WcfService8.gif