Consuming an ASMX Web Service

Once a Silverlight application has a service reference to an ASMX web service, it can invoke the web service through a proxy object. The basic steps for invoking a web service are to add the service reference, create the instance of the proxy object, add an event handler for its completion event, and invoke the web service asynchronously.

Bindings Again

The Silverlight application contains four TextBox controls that represent the To address, From address, Subject, and Body of the email message. The To address can contain a series of email addresses delimited by a semicolon.

You can manually load the default values into the TextBox controls by setting each TextBox’s Text property value. After the user has entered the values, you can retrieve the values from the TextBox controls to pass them to the web service’s web method by manually pulling the values from the controls and into local variables.

Although this technique works, another option is to take advantage of the XAML-based binding techniques. A MessageInfo class exists in the example code for the purpose of storing the values from these fields and acting as a mediator for the TextBox controls. Example 5-3 shows the code for the MessageInfo class. An instance of the MessageInfo class is created and the default values are initialized in the constructor of the EmailClient Silverlight 2 control. Example 5-4 shows a snippet of the control’s XAML and its bindings.

Example 5-3. MessageInfo class ...

Get Data-Driven Services with Silverlight 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.