To call a web service from code-behind a service client has to be generated in a Code Block design control.
For this example let’s assume that a web service has been created and published (along with its WSDL metadata description) at this URL: http://www.serviceprovider.com/TestService/service.svc
The test service provides single contract with one operation (method) that returns an array of strings and is defined like this:
[ServiceContract] public interface ITestService { [OperationContract] string[] GetItemsList(); }
To prepare a web service client follow these steps:
1. Create a new project and add a new form.
2. Put a Code Block named NDCode1 on the form.
3. Put a Combo Box control on the form; leave the default name ComboBox1
4. In the code block click the Add Service button.
5. When the Add Service Reference dialog opens,
a) type http://www.serviceprovider.com/TestService/service.svc to the Address field
b) click the Go button and wait till the Services list is filled
c) select the ITestService service contract from the Services list
c) set the Name the Namespace fields to TestService
d) click the Generate button – if everything is OK, the service client is generated and added to the list of code holders in the Code Block
6. Click the New event button to open the Event Handler dialog.
a) Set the Name field to FillComboEvent.
b) Select eForm as the Target object.
c) Select Loaded in the Event name field.
d) Click the New button next to the Code fragment field.
e) Name the new code holder FillComboCode in the open Code Holder dialog.
f) Replace the default code snippet with this code:
public class Class01 { // on button click event handler method example public static void OnFormLoaded(object sender, System.Windows.RoutedEventArgs e) { // get the combo to fill items from the service to ComboBox combo = ThisForm.FindElement<ComboBox>("ComboBox1"); if (combo != null) { // create a service client - default constructor remembers // the endpoint URL from which the client was generated TestServiceClient client = new TestServiceClient(); // call the asynchronous method client.GetItemsListCompleted += new EventHandler<GetItemsListCompletedEventArgs>(client_GetItemsListCompleted); client.GetItemsListAsync(combo); } } static void client_GetItemsListCompleted(object sender, GetItemsListCompletedEventArgs e) { if(e.Error != null) { MessageBox.Show(e.Error.Message, "Error", MessageBoxButton.OK); return; } string[] items = e.Result as string[]; if (items != null) { ComboBox combo = e.UserState as ComboBox; combo.ItemsSource = items; } } }
g) Click the Submit button to save changes and close the dialog
h) Select the newly created code block in the Code fragment field of the Event Handler dialog
i) Fill in OnFormLoaded in the Event handler field.
j) Click the Submit button to finish creating the new event handler.
7. Save or publish the test form to the Form server. In the Render open the form and check the combo box that it is filled with the items from the service.