Question

I have a Silverlight project calling a WCF service. The service method GetData has one parameter, dataID, and is called through GetDataAsync and is handled by the autocreated classes with functions BeginGetData and EndGetData. In some cases the call will throw a TimeoutException in EndGetData (which is fine, the WCF service it self calls some third party services and sometimes they might be down or have problems, so I do not want to increase the timeout values).

What I would like to do is be able to take action using the dataID sent to GetDataAsync (for instance schedule a new call to GetData or showing an appropriate error message to the user indicating what data stream failed). How can this be done? If I set a breakpoint in EndGetData, the result parameter of type IAsyncResult will be a System.ServiceModel.Channels.ServiceChannel.SendAsyncResult object, which has a RPC property that I can find the parameter in when debugging, but this class is not available from code because it is declared Friend.

Is this at all possible or does someone have ideas for how to implement the behaviour I want in another fashion?

Was it helpful?

Solution

Rather implement the Completed EventHandler that should have been auto generated by Visual Studio. You can also invoke using your own userState value.

void GetData(int dataID)
{
  client.GetDataCompleted += GetDataCompleted;
  client.GetDataAsync(dataID, dataID); //the 2nd param being the userState object
}

void GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
  var dataId = (int)e.UserState;
}

If an error occurred, the EventArgs will also contain the exception:

if (e.Error != null)
  throw e.Error;

Avoid changing/using the auto generated methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top