Partager via


How to: 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 <enableWebScript> endpoint behavior. After configuring the endpoint, the steps to implement and host the service are similar to those used by any WCF service. For a working example, see the AJAX Service Using HTTP POST sample.

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

  1. Define a basic WCF service contract with an interface marked with the ServiceContractAttribute attribute. Mark each operation with the OperationContractAttribute. 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…
    
    }
    
  2. Implement the ICalculator service contract with a CalculatorService.

    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
    
    //Other operations omitted…
    
  3. Define a namespace for the ICalculator and CalculatorService 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

  1. Create a behavior configuration and specify the <enableWebScript> behavior for ASP.NET AJAX-enabled endpoints of the service.

    <system.serviceModel>
        <behaviors>
            <endpointBehaviors>
                <behavior name="AspNetAjaxBehavior">
                    <enableWebScript />
                </behavior>
            </endpointBehaviors>
        </behaviors>
    </system.serviceModel>
    
  2. Create an endpoint for the service that uses the WebHttpBinding and the ASP.NET AJAX behavior defined in the previous step.

    <system.serviceModel>
        <services>
            <service name="Microsoft.Ajax.Samples.CalculatorService">
                <endpoint address=""
                    behaviorConfiguration="AspNetAjaxBehavior" 
                    binding="webHttpBinding"
                    contract="Microsoft.Ajax.Samples.ICalculator" />
            </service>
        </services>
    </system.serviceModel> 
    

To host the service in IIS

  1. 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 @ServiceHost directive 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"
    %>
    
  2. For more information about hosting in IIS, see How to: Host a WCF Service in IIS.

To call the service

  1. 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/<operation> - 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 sample.

See Also

Tasks

How to: Migrate AJAX-Enabled ASP.NET Web Services to WCF

Concepts

Creating WCF Services for ASP.NET AJAX


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07