Bug 8742 - WebClient.DownloadDataAsync Exception handling
Summary: WebClient.DownloadDataAsync Exception handling
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-12-04 03:35 UTC by Thomas Guzman
Modified: 2012-12-04 03:35 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Thomas Guzman 2012-12-04 03:35:06 UTC
The way System.Net.WebClient.DownloadDataAsync catch Exceptions make the class to fire event twice if an exception is thrown in the event handler, making debugging very difficult (Exceptions are not thrown, catched by the WebClient), and unexpected results.

Original code:

object[] array = (object[])state;
try
{
	byte[] result = this.DownloadDataCore((Uri)array[0], array[1]);
	this.OnDownloadDataCompleted(new DownloadDataCompletedEventArgs(result, null, false, array[1]));
}
catch (Exception ex2)
{
	bool cancelled = false;
	WebException ex = ex2 as WebException;
	if (ex != null)
	{
		cancelled = (ex.Status == WebExceptionStatus.RequestCanceled);
	}
	this.OnDownloadDataCompleted(new DownloadDataCompletedEventArgs(null, ex2, cancelled, array[1]));
}


Suggestion:

object[] array = (object[])state;
byte[] result = null;
WebException ex = null;
bool cancelled = false;
object userState = null;
try
{
	if (array.Length > 1)
	{
		userState = array[1];
	}
	result = this.DownloadDataCore((Uri)array[0], userState);
}
catch (Exception ex2)
{
	result = null;
	ex = ex2 as WebException;
	if (ex != null)
	{
		cancelled = (ex.Status == WebExceptionStatus.RequestCanceled);
	}
}
this.OnDownloadDataCompleted(new DownloadDataCompletedEventArgs(result, ex, cancelled, userState));

Note You need to log in before you can comment on or make changes to this bug.