Reader Level:

WCF Service using ASP.NET and Client Script

By Hemant Patil on Feb 25, 2009
WCF Service using ASP.NET and Client Script

Step 1: Create an ASP.NET 3.5 website.


Step 2: Right click the project > Add New Item > Choose 'AJAX-enabled WCF Service' from the Templates > name the service as 'GreetingService' > Add.


Step 3: Open the GreetingService.cs to implement operations to be exposed in the service. Specify the Namespace for ServiceContractAttribute as 'GreetNM':


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.


Step 4: 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.


Step 5: 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:


Step 6: 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.


Step 7 : 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.

<%@ 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 8: Resultant Window as Show in Bellow.




Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.
Infragistics jQuery Controls

Trending up