To create the ASP.NET client application
-
Open Visual Studio 2012.
-
From the File menu, select New, then Project, then Web, and then select ASP.NET Web Application.
-
Name the Project SandwichServices and click OK.
To create the WCF AJAX-enabled service
-
Right-click the SandwichServices project in the Solution Explorer window and select Add, then New Item, and then AJAX-enabled WCF Service.
-
Name the service CostService in the Name box and click Add.
-
Open the CostService.svc.cs file.
-
Specify the Namespace for ServiceContractAttribute as SandwichService:
namespace SandwichServices { [ServiceContract(Namespace = "SandwichServices")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class CostService { … } }-
Implement the operations in the service. Add the OperationContractAttribute
to each of the operations to indicate that they are part of the
contract. The following example implements a method that returns the
cost of a given quantity of sandwiches.
public class CostService { [OperationContract] public double CostOfSandwiches(int quantity) { return 1.25 * quantity; } // Add more operations here and mark them with [OperationContract] }To configure the client to access the service
-
Open the Default.aspx page and select the Design view.
-
From the View menu, select Toolbox.
-
Expand the AJAX Extensions node and drag and drop a ScriptManager on to the Default.aspx page.
-
Right-click the ScriptManager and select Properties.
-
Expand the Services collection in the Properties window to open up the ServiceReference Collection Editor window.
-
Click Add, specify CostService.svc as the Path referenced, and click OK.
-
Expand the HTML node in the Toolbox and drag and drop an Input (Button) on to the Default.aspx page.
-
Right-click the Button and select Properties.
-
Change the Value field to Price for 3 Sandwiches.
-
Double-click the Button to access the JavaScript code.
-
Pass in the following JavaScript code within the <script> element.
function Button1_onclick() { var service = new SandwichServices.CostService(); service.CostOfSandwiches(3, onSuccess, null, null); } function onSuccess(result){ alert(result); }
To access the service from the client
-
Use Ctrl +F5 to launch the service and the Web client. Click the Price for 3 Grilled Sandwiches button to generate the expected output of "3.75".
Example
This example contains the service code contained in the WCFService.svc.cs file and the client code contained in the Default.aspx file.
//The service code contained in the CostService.svc.cs file. using System; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; namespace SandwichServices { [ServiceContract(Namespace="SandwichServices")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class CostService { // Add [WebGet] attribute to use HTTP GET [OperationContract] public double CostOfSandwiches(int quantity) { return 1.25 * quantity; } // Add more operations here and mark them with [OperationContract] } } //The code for hosting the service is contained in the CostService.svc file. <%@ ServiceHost Language="C#" Debug="true" Service="SandwichServices.CostService" CodeBehind="CostService.svc.cs" %> //The client code contained in the Default.aspx file. @ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SandwichServices._Default"Untitled Page function Button1_onclick() { var service = new SandwichServices.CostService(); service.CostOfSandwiches(3, onSuccess, null, null); } function onSuccess(result){ alert(result); }//Here add a Service Reference file and and input button in the .Aspx file and
call the above java function.
-
Open the Default.aspx page and select the Design view.