How to : Use the UserState property while making async WCF service calls

this code snippet decribes how to use the UserState property while making async WCF service calls in silverlight

In Silverlight, all the WCF service calls are asynchronous. After the call to the WCF service is completed, a Completed event is called at the silverlight UI side.Sometimes it becomes necessary to determine which task raised the Completed event.In this case you can use the "UserState" property to determine this.

For Example:

You want to download files from the server, and when the download is complete you want to show the filename that was downloaded. In the sample below "DownloadFileAsync" is the WCF function that is called asynchronously, "webServiceClient_DownloadFileCompleted" is the completed event for the WCF service call, "Filename" has the filename that is downloading.

private void DownloadSingleFile()
// Set user state here
webServiceClient.DownloadFileAsync(new Uri(downloadUrl), Filename);

void webServiceClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
txtStatus.Text = "File : " + e.UserState + " downloaded uccessfully !";


Note: The "UserState" property is of "Object" type so you can pass variable of any datatype.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: