Hello GT,
Thanks for sending me the project.
I tested it and found that the exception was actually thrown by calling the
UploadFileCompletedEventArgs.Result Property in the following line:
txt_Info.Text += System.Text.Encoding.UTF8.GetString(e.Result) + "\r\n";
For the WebException thrown in the WebClient.UploadFileAsync method, it
should be trapped in the UploadFileComplete event callback. If there is an
error uploading the file, this event will be fired, and the Error property
of the
UploadFileCompletedEventArgs(
http://msdn.microsoft.com/en-us/library/system.
net.uploadfilecompletedeventargs(VS.80).aspx) passed to this event callback
method will contain an exception. This mechanism is different with normal
.Net exceptions since the method is executed asynchronously.
Under this situation, the Result Property of UploadFileCompletedEventArgs
will not be valid. The get_Result method will call the
RaiseExceptionIfNecessary method of the base class of
UploadFileCompletedEventArgs to throw a TargetInvocationException. The
following code was got using .net
reflector(
http://www.aisto.com/roeder/dotnet) and showed this logic.
public byte[] get_Result()
{
base.RaiseExceptionIfNecessary();
return this.m_Result;
}
protected void RaiseExceptionIfNecessary()
{
if (this.Error != null)
{
// This is the exception thrown in Visual Studio.
throw new
TargetInvocationException(SR.GetString("Async_ExceptionOccurred"),
this.Error);
}
if (this.Cancelled)
{
throw new
InvalidOperationException(SR.GetString("Async_OperationCancelled"));
}
}
In addition, from the MSDN document of UploadFileCompletedEventArgs.Result
Property, we can see the following remarks:
You should check the Error and Cancelled properties to determine whether
the upload completed. If the Error property's value is an Exception object
or the Cancelled property's value is true, the asynchronous operation did
not complete correctly and the Result property's value will not be valid.
I modified the UploadFileCallback. Now we can catch the WebException and
the application will not crash any more.
public void UploadFileCallback(Object sender, UploadFileCompletedEventArgs
e)
{
if(e.Cancelled)
{
txt_Info.Text += "Cancelled\r\n";
}
else if (e.Error != null)
{
// There's an error thrown inside the UploadFileAsync method.
txt_Info.AppendText(e.Error.Message + Environment.NewLine);
txt_Info.AppendText(e.Error.InnerException.Message +
Environment.NewLine);
}
else
{
txt_Info.AppendText(System.Text.Encoding.UTF8.GetString(e.Result) +
Environment.NewLine);
txt_Info.AppendText( "Finish" + Environment.NewLine);
}
btn_Up.Enabled = true;
}
Please let me know if you have any other concerns or need any other help.
Best regards,
Feng Chen
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(E-Mail Removed).
This posting is provided "AS IS" with no warranties, and confers no rights.