小编exe*_*exe的帖子

处理“任务被取消”异常

我的 C# 应用程序将文件上传到某个 API,我使用的是多部分请求,即我正在上传文件的 json 字符串和二进制内容,它适用于大多数文件,但对于极少数文件,它给出了例外,我的意思是让我们尝试对于名为50MB.zip 我的文件,出现异常:

A task was canceled. :::  ::: System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
Run Code Online (Sandbox Code Playgroud)

我的代码大致如下:

public async Task<Dictionary<string , string>> Upload(string filePath)
{   
    FileInfo fi = new FileInfo(FilePath);
    string jsonString="some json string";
    byte[] fileContents=File.ReadAllBytes(fi.FullName);
    Uri webService = new Uri(url);
    HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);
    requestMessage.Method = HttpMethod.Post;
    requestMessage.Headers.Add("Authorization" , "MyKey1234");
    const string boundry = "------------------My-Boundary";
    MultipartFormDataContent multiPartContent = new MultipartFormDataContent(boundry);
    ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents); …
Run Code Online (Sandbox Code Playgroud)

c# async-await

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

async-await ×1

c# ×1