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 :
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.