Partager via


Accessing ASP.NET AJAX Services

Microsoft Silverlight will reach end of support after October 2021. Learn more.

This topic describes considerations when accessing Windows Communication Foundation (WCF) AJAX services, ASP.NET AJAX services, and some built-in services, such as the profile application service, the roles service, and the authentication service provided by ASP.NET.

You can access most services that are used by ASP.NET AJAX Web pages from Silverlight applications. For example, if you are adding Silverlight content to an existing Web site that is built using the ASP.NET AJAX technology, you can usually do so by writing only new client Silverlight code instead. You do not have to change any code on the server, although you may have to change some configuration for the service on the server, as described later in this document, to enable Silverlight accessibility.

In ASP.NET AJAX, you use services by adding them to the Services collection of the ScriptManager control. These services are one of two types:

  • WCF AJAX services: These are services based on the WCF technology. They are usually identified by the presence of a characteristic ".svc" extension in their URL. These services are usually created using the AJAX-enabled WCF Service item template in Visual Studio 2010.

  • ASMX AJAX services: These are services based on the ASP.NET Web Services technology. They are usually identified by the presence of a characteristic ".asmx" extension in their URL and by the use of the System.Web.Script.Services.ScriptServiceAttribute and System.Web.Script.Services.ScriptMethodAttribute attributes.

Accessing WCF AJAX Services from Silverlight

To access a WCF AJAX service, you must ensure that it is configured to have a Silverlight-compatible endpoint:

  1. Find the appropriate configuration file (normally the Web.config file in the same directory as the .svc file).

  2. Add the appropriate endpoint, either by using a tool or manually.

    1. To add the endpoint by using a tool:

      1. Open the Web.config file in the Service Configuration Editor from the Tools menu of Visual Studio.

      2. Add a new “Silverlight” endpoint that is configured with the BasicHttpBinding by right-clicking on the Endpoints section in the Service section of the Configuration graph and selecting New Service Endpoint.

      3. If your existing AJAX endpoint had any security settings enabled, make sure to add them to the newly created endpoint as well.

      4. Turn on metadata publishing for the service in the Advanced -> Service Behaviors section. The <behaviors> element for the service should then contain the <serviceMetadata> element configured as follows.

        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        
    2. To add the endpoint manually:

      1. Add the following element to the Web.config configuration file inside the scope of the <system.serviceModel><services><service> elements.

        <endpoint address="silverlight" 
                       binding="basicHttpBinding" 
                       contract="…" />
        
      2. The "address" can be anything you choose. The "contract" should be the same as used for your existing AJAX <endpoint> element.

      3. If your existing AJAX <endpoint> element had any security settings enabled, make sure to add them to the newly created <endpoint> element as well.

      4. Turn on metadata publishing for the service by adding one more <endpoint> element.

        <endpoint address=”mex” 
                       binding=”mexHttpBinding”
                      contract=”IMetadataExchange”/>
        
  3. Use the Add Service Reference tool in Visual Studio 2010 in your Silverlight project to use the service you just configured. The procedures for using this tool are described in How to: Access a Service from Silverlight.

Accessing ASMX AJAX Services from Silverlight

Normally, ASMX AJAX services do not require any additional configuration to be usable from Silverlight. Use the Add Service Reference tool in Visual Studio 2010 in your Silverlight project to use the service you just configured, as described in How to: Access a Service from Silverlight.

However, if you are using some more advanced features, additional changes are required:

  • If your ASMX AJAX service is using the T:System.Collections.Generic.Dictionary(TKey, TValue) type to represent arbitrary JSON data, note that the feature is not supported in Silverlight. You must rewrite your service to avoid using this type.

  • If you configured ASP.NET to turn off SOAP support for .asmx Web services, you must remove this configuration for these services to work with Silverlight.

Accessing Built-in Services from Silverlight

ASP.NET provides several services: for example, the ASP.NET profile application service, the roles service, and the authentication service. These services can be exposed using either the JSON data format or the SOAP data format (or both). Using the Add Service Reference feature, Silverlight can only access these services using the SOAP data format. For more information about how to enable these services and the SOAP format in particular, see the ASP.NET documentation.

Services that Cannot be Accessed

The following ASP.NET AJAX functionality cannot be accessed from Silverlight using the Add Service Reference feature:

  • Page methods used to bind server code to ASP.NET AJAX pages.

  • Any partial-page rendering functionality enabled by the ASP.NET UpdatePanel control.