0

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?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Johan
  • 780
  • 7
  • 22

1 Answers1

1

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.

Rei Mavronicolas
  • 1,387
  • 9
  • 12
  • I have implemented the EventHandler and that is where I handle correct results and errors thrown from the service, the problem is the dataId does not come with the GetDataCompletedEventArgs (e.UserState is null) when the error is a TimeoutException. – Johan Jun 13 '12 at 09:31
  • Hmm strange. I just tried one of my WCF operations, mine times out after 30 seconds, hits the Completed event and the UserState is persisted. Are you explicitly specifying the dataId as the userState object when invoking GetDataAsync? – Rei Mavronicolas Jun 13 '12 at 10:23