.NET error when calling System.Net.WebClient.UploadFileAsync

I am working with the System.Net.WebClient class and I am trying to upload a file using the UploadFileAsync method. I use Visual Studio 2010, and all my projects are configured to use the standard .NET 4.0 runtime, not the client library.

Below is a small section of the code I'm using. In about 90% of cases, I get the following error:

Unable to pass object of type "System.ComponentModel.AsyncOperation" to enter "UploadBitsState".

 Stack Trace: at System.Net.WebClient.UploadFileAsyncWriteCallback(Byte[] returnBytes, Exception exception, Object state) at System.Net.WebClient.UploadFileAsync(Uri address, String method, String fileName, Object userToken) at FileUpload._StartUpload() 

The FTP servers I am trying to download are internal to my organization, but one uses IPSwitches WS-FTP and the other uses the IIS 6.0 FTP site, and I encountered the same problem with both servers.

I searched high and low for others with a similar problem to no avail.

The actual line where the exception occurs is a call to the _Client.UploadFileAsync method.

 private void _StartUpload() { try { _Client = new WebClient { Credentials = _Credentials }; _Client.UploadProgressChanged += ProgressChanged; _Client.UploadFileCompleted += UploadCompleted; _Client.UploadFileAsync(FileBeingUploaded, "STOR", _LocalFile, null); } catch (Exception exception) { // Methods calls removed for brevity } } private void UploadCompleted(Object sender, UploadFileCompletedEventArgs e) { // Methods calls removed for brevity } private void ProgressChanged(object sender, UploadProgressChangedEventArgs e) { // Methods calls removed for brevity } 
+6
source share
1 answer

It is interesting. Looking at the source (WebClient.cs), the first line of UploadFileAsyncWriteCallback passes the state parameter to UploadBitsState .

The UploadFileAsync method has an exception handling code that reads:

 catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if(fs != null){ fs.Close(); } if (!(e is WebException || e is SecurityException)) { e = new WebException(SR.GetString(SR.net_webclient), e); } UploadFileAsyncWriteCallback(null, e, asyncOp); } 

asyncOp is of type AsyncOperation .

It seems that calling UploadFileAsyncWriteCallback here is an error, as it passes the wrong type object to the callback. The callback performs a C-style listing (i.e. UploadBitsState uploadState = (UploadBitsState)state; ).

But this will only happen if something throws an exception at boot time.

Is it possible that something in your ProgressChanged or UploadCompleted event handlers throws an exception? Either this, or one of the parameters that you pass to UploadFileAsync is invalid.

Additional Information

Actually, it looks like a bug in UploadFileAsync . For example, the following throws an InvalidCastException when, according to the documentation, it should throw a WebException .

 var targetUri = new Uri("ftp://example.com/file.txt"); var srcFile = string.Empty; // documentation says this will throw WebException var client = new WebClient(); client.UploadFileAsync(targetUri, "STOR", srcFile, null); 

I reported an error at https://connect.microsoft.com/VisualStudio/feedback/details/675575/webclient-uploadfileasync-throws-invalidcastexception

However, in terms of things, I would say that the reason the exception is thrown lies in your code. Unfortunately, it is impossible to say where, because UploadFileAsync loses exception information. Perhaps, as someone else noted, an attempt to synchronize download would shed more light on this topic.

+4
source

Source: https://habr.com/ru/post/890533/


All Articles