Udostępnij za pośrednictwem


How to: Configure WCF Services in ASP.NET AJAX

Windows Communication Foundation (WCF) enables you to create a service (.svc) that can be called from client ECMAScript (JavaScript) functions that run in an ASP.NET AJAX-enabled Web application. This topic explains how to configure the service so that you can call it from client script. You configure the service in the system.serviceModel element of the Web.config file, which is a child of the configuration section.

During WCF service calls, the JSON format is used to exchange data between client application and the service.

To configure WCF services to enable calls from client script

  1. Open the application's Web.config file.

  2. Set the aspNetCompatibilityEnabled of the serviceHostingEnvironment element to true, as shown in the following example:

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    
  3. Configure the messaging stack to be applied to the HTTP pipeline as shown in the following example:

    <bindings>
        <webHttpBinding>
           <binding name="default"/>
        </webHttpBinding>
    </bindings>
    
  4. Configure the behavior element for the endpoints and services by defining the endpointBehaviors and the serviceBehaviors that is contained by the behaviors element.

    The behavior element for the endpointBehaviors element must be configured so that Web scripting is enabled. The behavior element for the serviceBehaviors element must be configured so that the service metadata is published for retrieval by using an HTTP GET request.

    The following example shows how to configure the behavior and endPointBehavior elements.

    <behaviors>
      <endpointBehaviors>
        <behavior name="webScriptEnablingBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
    
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  5. Configure an endpoint for the service element whose webHttpBinding and behavioConfiguration attribute values were defined in the previous steps.

    The following example shows how to configure the service element.

    <services>
      <service name="Samples.Aspnet.SimpleService"
         behaviorConfiguration="MyServiceTypeBehaviors">
         <endpoint address="" binding="webHttpBinding"
           bindingConfiguration="default"
           contract="Samples.Aspnet.ISimpleService"
           behaviorConfiguration="webScriptEnablingBehavior"/>
      </service>
    </services>
    

See Also

Concepts

Exposing WCF Services to Client Script

Other Resources

<system.serviceModel>

Creating WCF Services for ASP.NET AJAX

How to: Use Configuration to Add an ASP.NET AJAX Endpoint

Support for JSON and Other Data Transfer Formats