Use Configuration to Add an ASP.NET AJAX Endpoint
Windows Communication Foundation (WCF) allows you to create a service that makes an ASP.NET AJAX-enabled endpoint available that can be called from JavaScript on a client Web site. To create such an endpoint, you can either use a configuration file, as with all other Windows Communication Foundation (WCF) endpoints, or use a method that does not require any configuration elements. This topic demonstrates the configuration approach.
The part of the procedure that enables the service endpoint to become ASP.NET AJAX-enabled consists of configuring the endpoint to use the WebHttpBinding and to add the
For more information about how to configure an ASP.NET AJAX endpoint without using configuration, see How to: Add an ASP.NET AJAX Endpoint Without Using Configuration.
To create a basic WCF service
Define a basic WCF service contract with an interface marked with the ServiceContractAttribute attribute. Mark each operation with theOperationContractAttribute. Be sure to set the Namespace property.
[ServiceContract(Namespace = "MyService")] public interface ICalculator { [OperationContract] // This operation returns the sum of d1 and d2. double Add(double n1, double n2); //Other operations omitted… }
Implement the
ICalculator
service contract with aCalculatorService
.public class CalculatorService : ICalculator { public double Add(double n1, double n2) { return n1 + n2; } //Other operations omitted…
Define a namespace for the
ICalculator
andCalculatorService
implementations by wrapping them in a namespace block.Namespace Microsoft.Ajax.Samples { //Include the code for ICalculator and Caculator here. }
To create an ASP.NET AJAX endpoint for the service
To host the service in IIS
To host the service in IIS, create a new file named service with a .svc extension in the application. Edit this file by adding the appropriate @ServiceHostdirective information for the service. For example, the content in the service file for the
CalculatorService
sample contains the following information.<%@ServiceHost language=c# Debug="true" Service="Microsoft.Ajax.Samples.CalculatorService" %>
For more information about hosting in IIS, see How to: Host a WCF Service in IIS.
To call the service
The endpoint is configured at an empty address relative to the .svc file, so the service is now available and can be invoked by sending requests to service.svc/
- for example, service.svc/Add for the Add
operation. You can use it by entering the endpoint URL into the Scripts collection of the ASP.NET AJAX Script Manager control. For an example, see the AJAX Service Using HTTP POST.
Comments
WCF Training | WCF Online Training | Online WCF Training | WCF Training in chennai | Dot Net Training Institutes in Chennai | Dot Net Training in Chennai | Online Dot Net Training