Configuring Web Service Usage in Silverlight Clients

Silverlight version 2 provides a rich platform to write Internet applications. One of its key features is to enable the applications to communicate with Web services using generated proxies. Silverlight 2 has added Web client configuration support that enables deployment-time configuration, such as changing the address of the targeted service endpoints or various settings without having to rebuild and redeploy the binaries.

For Silverlight 2 applications, the client configuration file contains a subset of Windows Communication Foundation (WCF) client configuration. The Silverlight 2 configuration file must be named ServiceReferences.ClientConfig, and also must be packaged and deployed along with the application. The XAP file is a container of other files, similar to a Compressed Folder (“.zip”). If configuration needs to change at deployment time, it is possible to uncompress the XAP file, change the desired configuration setting, and compress the results into a new XAP file, all without recompiling the application.

Configuring a Silverlight Client

The Add Service Reference tool automatically generates the client configuration that is contained in the ServiceReferences.ClientConfig file.

Note that this file has a subset of the contents of the .NET Framework 3.0 configuration file that is used to configure the client proxy. the Windows Communication Foundation (WCF) client configuration file, see >Configuring Client Behaviors. Configuration consists of a <bindings> section and a <client> section, as can be seen in the following example. The sections are contained within the <system.serviceModel> element. The elements that configure the CustomerClient client to access the Service1 Web service that has an IService contract are contained within the <system.serviceModel> element, which in turn is contained in a <configuration> element.

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding 
         name="BasicHttpBinding_IService" 
         closeTimeout="00:59:00"
         openTimeout="00:53:00" 
         receiveTimeout="00:15:00" 
         sendTimeout="00:21:00"    
         maxBufferSize="65530"  
         maxReceivedMessageSize="65531" 
         textEncoding="utf-8"
         <security mode="None" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
       <endpoint 
        address="http://localhost:61424/WebSite5/Service1.svc"
        binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IService"
        contract="CustomerClient.CustomerService.IService"
        name="BasicHttpBinding_Service" />
    </client>
  </system.serviceModel>
</configuration>

Configuring the Service Address

The address attribute of the service <endpoint> element identifies the address of the service. You can change the value of the address attribute when the service moves. For example, if the service moves to http://example.com/Service1.svc, you would change the <endpoint> as follows:

<endpoint 
   address="http://example.com/Service1.svc"
   binding="basicHttpBinding"
   bindingConfiguration="BasicHttpBinding_IService"
   contract="CustomerClient.CustomerService.IService"
   name="BasicHttpBinding_Service" />

Note that HTTP and HTTPS URLs are both supported, but the security mode must be properly configured. See the Configuring the Security Mode section below.

If a service has more than one Silverlight-compatible endpoint, multiple <endpoint> elements might be present with the same contract attribute. The endpoints have different endpoint names, identified by the name attribute. Typically, the endpoints are distinguished by having different addresses specified by their respective address attributes. For example:

<client>
   <endpoint 
      address="http://uk.example.com/Service1.svc"
      binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService"
      contract="CustomerClient.CustomerService.IService"
      name="EuropeDataCenter" />
   <endpoint 
      address="http://jpn.example.com/Service1.svc"
      binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService"
      contract="CustomerClient.CustomerService.IService"
      name="AsiaDataCenter" />
    </client>

If multiple endpoints are configured, it is important to specify the endpoint name as a constructor argument when creating the proxy. For example, var p = new CustomerServiceClient(“EuropeDataCenter”); If you do not do this, an exception will occur. Also, an exception will occur if no endpoints are present in configuration and you attempt to use a proxy constructor that takes an endpoint name or that takes no parameters.

Configuring Quotas, Time-outs, and Encoding

The <binding> element specifies how the Silverlight client will communicate with the service. The basicHttpBinding is the only binding that can be configured in Silverlight version 2, and corresponds to the BasicHttpBinding that enables SOAP 1.1 over HTTP (or HTTPS).

The bindingConfiguration attribute specifies, by name, a particular configuration that affects how to communicate with the service. These configurations are found in the <binding> elements under the <basicHttpBinding> element in the <bindings> section. The point is that there might be more than one such <binding> element and they are identified distinct name attributes.

You can change quota and time-out settings for the client using the binding configuration. For example, you can change the value of the maximum size for the buffer that receives messages by specifying a new value for the maxBufferSize binding attribute, or you can change the value of the maximum size for a message that can be received by specifying a new value for the maxReceivedMessageSize. The values are specified in bytes. For example, to receive messages as large as 1 megabyte, you might use the following:

<binding 
   name="BasicHttpBinding_IService" 
   closeTimeout="00:59:00"
   openTimeout="00:53:00" 
   receiveTimeout="00:15:00" 
   sendTimeout="00:21:00"
   maxBufferSize="1048576" 
   maxReceivedMessageSize="1048576"textEncoding="utf-8" >
   <security mode="None" />
</binding>

The bound on the received message size is intended to limit exposure to DoS-style attacks. Note also that if you want to send large messages from Silverlight, it is the receiving side (that is, the service) that must be configured with sufficiently large quotas. For more information, see Security Considerations for Service Access.

Binding configuration also enables you to configure time-outs for opening a channel to the service, sending a message, receiving a reply, and closing a channel. The values are specified in the hours:minutes:seconds format.

Last, the textEncoding attribute enables you to configure the character encoding (for example, UTF8 or UTF16) to be used for sending messages. You need to ensure that the service you are communicating with supports the character encoding you choose.

<binding name="BasicHttpBinding_IService" 
   closeTimeout="00:59:00" 
   openTimeout="00:53:00" 
   receiveTimeout="00:15:00" 
   sendTimeout="00:21:00" 
   maxBufferSize="65530" 
   maxReceivedMessageSize="65531" 
   textEncoding="utf-16" > 
   <security mode="None" />
</binding>

Configuring the Security Mode

The <security> element inside a <binding> element specifies the security mode to be used with the given binding configuration. In silverlight, the security mode must be configured as follows: For HTTP addresses: <security mode="None" />. For HTTPS addresses: <security mode="Transport" />.

See Also

Send comments about this topic to Microsoft.

Tags :


Page view tracker