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

Gokul

New member
Joined
Oct 30, 2024
Messages
2
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: 2
You already have a thread about this. Merging threads...
 
Back
Top Bottom