Message rejection
When your Silverlight application that is running on the web client sends a message to the server, such as a user updating the value of a control, there is a remote possibility that the message will be rejected by the server. A message could be rejected if the server is in a state that doesn't allow it to process messages. When the message is rejected, the message is sent back to the Silverlight application where it can be handled by the Revert() method that you implement.
When a message is rejected by the server, the web client will be out of synch with the status of the application on the server. When the Revert() method receives the message that was rejected by the server, the code in the method must set the state of the Silverlight application back to the way it was before the message had been sent.
To be able to restore the previous state of the application, your Silverlight application will need to keep track of the value or values as they existed before you sent the message to update the server. For example, if you sent a message to update the server when the user changes a value in a text box control, you should keep track of the previous value of the text box control. If the message is rejected, you can restore the text box control to its previous value.
The following C# example shows the SelectionChanged event for the data grid control in the Silverlight component of the Salesperson Analysis sample. This event occurs when the user selects a row in the data grid. Notice how the previous selection is stored so that it can be used if needed.
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) { // The user changed the selected customer. Send the updated selection // back to the server. // Store the object that indicates the previous selection, in case the // message is rejected if (e.RemovedItems.Count != 0) { // Save the previous selection GPWCAddIn.prevSelection = e.RemovedItems; } else { // No item was previously selected GPWCAddIn.prevSelection = null; } // Get the selection int selection = GPWCAddIn.saWindow.dataGrid1.SelectedIndex; // Convert the string value to encoded bytes to send byte[] bytedata; System.Text.UTF8Encoding val = new UTF8Encoding(); bytedata = val.GetBytes(selection.ToString()); // Send the value to the server GPWCAddIn.Proxy.SendMessage(new WCVstCustomMessage( "SalespersonAnalysisSL.GPWCAddIn", "SalespersonAnalysis.GPAddIn", "SetSelectedCustomer", bytedata)); }
The following C# example shows the Revert() method for the Silverlight component of the Salesperson Analysis sample. If the "SetSelectedCustomer" message was rejected by the server, the previous value for the data grid is restored.
void IWCCustomAddIn.Revert(WCVstCustomMessage message) { // A message was rejected by the server. Examine the message returned, // and if necessary, revert the Silverlight application back to the way it // was before the message was sent. // The user selected a customer, but the message was rejected by the // server. if (message.Description == "SetSelectedCustomer") { // Reset the selection back to the previous value if (GPWCAddIn.prevSelection != null) { saWindow.dataGrid1.SelectedItem = GPWCAddIn.prevSelection[0]; } else { saWindow.dataGrid1.SelectedIndex = (-1); } } }