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. 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.
In ASP.NET AJAX, you use services by adding them to the Services collection of the ScriptManager control. These services are 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 2008.
-
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.
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:
-
Find the appropriate configuration file (normally, Web.config in the same directory as the .svc file).
- Add the appropriate endpoint, either by using a tool or manually.
- To add the endpoint by using a tool:
-
Open the file in the WCF Service Configuration Editor tool.
-
Add a new endpoint that is configured with the BasicHttpBinding.
-
If your existing AJAX endpoint had any security settings enabled, make sure to add them to the newly created endpoint as well.
-
Turn on metadata publishing for the service.
- To add the endpoint manually:
-
Add the following to the configuration file inside the scope of the <system.serviceModel><services><service> elements.
<endpoint address="silverlight" binding="basicHttpBinding" contract="…" />
-
The "address" can be anything you choose. The "contract" should be the same as used for your existing AJAX <endpoint> element.
-
If your existing AJAX <endpoint> element had any security settings enabled, make sure to add them to the newly created <endpoint> element as well.
-
Turn on metadata publishing for the service by adding one more <endpoint> element.
<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange”/>
-
Use the Add Service Reference tool in Visual Studio 2008 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 using Visual Studio.
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 2008 in your Silverlight project to use the service you just configured, as described in How to: Access a Service from Silverlight using Visual Studio.
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, the feature is unsupported 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.
-
Any functionality enabled by the ASP.NET UpdatePanel control.