BackgroundUploader thrown exception when response status code is 204 - No content

Gokul

Member
Joined
Oct 30, 2024
Messages
7
Programming Experience
1-3
I'm using BackgroundUploader and UploadOperation to upload a file to my server via API request, my API request is succeed with (204 - No content) status code, but a exception is thrown as `The operation was canceled by the user`. Rest of my API calls with (200) status code works good.

Code used - upload.StartAsync().AsTask(cts, progressCallback);

Need a fix.
 
My understanding is that you should be using:
C#:
await upload.StartAsync().AsTask(cts.Token, progressCallback);

But it maybe simply that you decide to not show us the await and that your cts is was truly the token, and not the token source.

Anyway, the first thing I would check is the value of cts.IsCancelRequested.

If it is set to true, then there you have it: the operation was truly cancelled by your code. Perhaps you are recycling a cts somewhere?

If it is set to false, then you'll need to do some troubleshooting to verify that connection is still valid. You may need to fire up WireShark or the equivalent.
 
I'm using BackgroundUploader and UploadOperation to upload a file to my server via API request, my API request is succeed with (204 - No content) status code, but a exception is thrown as `The operation was canceled by the user`. Rest of my API calls works good which returns with response status code like (200, 401, 401).

Code used :
Upload File:
private async Task<string> HandleUploadAsync(UploadOperation upload, CancellationTokenSource cts)
{
    string responseBody = string.Empty;
    try
    {
        Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress);
        UploadOperation operation;

        if(cts == null)
        {
            cts = new CancellationTokenSource();
        }

        var ct = CommonUtil.GetCancellationToken(cts);
       
        operation = newUpload ? await upload.StartAsync().AsTask(ct, progressCallback) : await upload.AttachAsync().AsTask(ct, progressCallback);

        if(cts.IsCancellationRequested)
        {
            throw new NoInternetAccessException("No Internet Access");
        }
        else if(cts != null)
        {
            cts.Dispose();
        }

        using (var response = operation.GetResultStreamAt(0))
        {
            uint size = (uint)operation.Progress.BytesReceived;
            IBuffer buffer = new Windows.Storage.Streams.Buffer(size);
            if (size > 0)
            {
                var f = await response.ReadAsync(buffer, size, InputStreamOptions.None);

                using (var dr = DataReader.FromBuffer(f))
                {
                    responseBody = dr.ReadString(dr.UnconsumedBufferLength);
                }
            }
        }
       
        return responseBody;
    }
    catch (TaskCanceledException)
    {
        throw new NoInternetException("No Internet Access");
    }
    catch (Exception ex)
    {
        throw ex;
    }
}



Note : exception is thrown only for response returning with 204 - No content and CTS is not canceled.
 

Attachments

  • image (11).png
    image (11).png
    31.3 KB · Views: 5
You already have a thread about this. Merging threads...
 
I did. And you are still experiencing the same problem as your original thread, and hence why I merged them. This is just a progression of your original problem, not a brand new problem.
 
Out of curiosity, why is your API returning a 204? Is the uploader truly sending no data to your API?
 
Out of curiosity, why is your API returning a 204? Is the uploader truly sending no data to your API?
1. why is your API returning a 204?
Returning 204 is expected, since 204 response is commonly used response.

2. Is the uploader truly sending no data to your API?
Yes.


Case : I'm developing a meeting client service APP through UWP, I'm uploading a file to server. Once the file upload was completed the exception was thrown.
 
My understanding is that when a GET is sent to a web resource and there is nothing to return because nothing has changed, then a 204 can be sent back. But in this case, presumably the uploader is sending a PUT or a POST. Why would you return a 204 even if no data is sent? What if the intent of the PUT was to truncate the file if the file already exists?

Anyway It might help us more if you described the sequence of HTTP messages that your API is receiving from the uploader.

Anyway, it sounds from your description that that the entire file is uploaded, and only then is the exception thrown. Does the ex object show the callstack of where the exception was thrown from?
 
1. why is your API returning a 204?
Returning 204 is expected, since 204 response is commonly used response.

2. Is the uploader truly sending no data to your API?
Yes.


Case : I'm developing a meeting client service APP through UWP, I'm uploading a file to server. Once the file upload was completed the exception was thrown.

2. Is the uploader truly sending no data to your API?

No, the uploader contains a file which needs to be uploaded in server.
 
Last edited:
My understanding is that when a GET is sent to a web resource and there is nothing to return because nothing has changed, then a 204 can be sent back. But in this case, presumably the uploader is sending a PUT or a POST. Why would you return a 204 even if no data is sent? What if the intent of the PUT was to truncate the file if the file already exists?

Anyway It might help us more if you described the sequence of HTTP messages that your API is receiving from the uploader.

Anyway, it sounds from your description that that the entire file is uploaded, and only then is the exception thrown. Does the ex object show the callstack of where the exception was thrown from?

Method - POST is used.

Anyway It might help us more if you described the sequence of HTTP messages that your API is receiving from the uploader.
No HTTP messages can be captured, since the exception will be thrown.

Does the ex object show the callstack of where the exception was thrown from?
StackTrace shows that the exception was thrown at the line operation = newUpload ? await upload.StartAsync().AsTask(ct, progressCallback) : await upload.AttachAsync().AsTask(ct, progressCallback); code available in the thread.
 
Anyway It might help us more if you described the sequence of HTTP messages that your API is receiving from the uploader.
No HTTP messages can be captured, since the exception will be thrown.
Really? Then how did you know that your API returned a 204 vs a 200?

Since you own the API, you could have tracing on the server to see what HTTP messages it receives since you seem to also know what it sends back.

If you use Fiddler or Charles, you should be able to see the HTTP messages on the client side.
 
Does the ex object show the callstack of where the exception was thrown from?
StackTrace shows that the exception was thrown at the line operation = newUpload ? await upload.StartAsync().AsTask(ct, progressCallback) : await upload.AttachAsync().AsTask(ct, progressCallback); code available in the thread.

I asked what the stack trace of the ex message shows (e.g. ex.StackTrace, also if there is ex.InnerException.StackTrace), not what Visual Studio shows.
 
I asked what the stack trace of the ex message shows (e.g. ex.StackTrace, also if there is ex.InnerException.StackTrace), not what Visual Studio shows.
1. StackTrace : at A.UWP.Common.BGTransfer.BGTransferManager.<HandleUploadAsync>d__16.MoveNext() in D:\meeting\Source\AUWPCommon\BGTransfer\BGransferManager.cs:line 228

2. InnerException is null.

3. HResult : -2147023673
 
Back
Top Bottom