2017-01-11 56 views
1

我有以下功能將文件上傳到Drobox並返回到這些文件的共享鏈接。上傳到Dropbox無法正常工作.NET SDK

private async Task<string> Upload(DropboxClient dbx, string localPath, string remotePath) 
    { 
     const int ChunkSize = 4096 * 1024; 
     using (var fileStream = File.Open(localPath, FileMode.Open)) 
     { 
      if (fileStream.Length <= ChunkSize) 
      { 
       WriteMode mode = new WriteMode(); 

       FileMetadata fileMetadata = await dbx.Files.UploadAsync(remotePath, body: fileStream, mode: mode.AsAdd, autorename: true); 
       //set the expiry date 
       var existingDoc = await dbx.Files.GetMetadataAsync(remotePath); 
       if (existingDoc.IsFile) 
       { 
        var sharedLink = dbx.Sharing.ListSharedLinksAsync(remotePath); 
        var settings = new ListSharedLinksArg(remotePath); 
        ListSharedLinksResult listSharedLinksResult = await dbx.Sharing.ListSharedLinksAsync(remotePath); 
        if (listSharedLinksResult.Links.Count > 0) 
        { 
         return listSharedLinksResult.Links[0].Url; 
        } 
        else 
        { 
         var settings2 = new SharedLinkSettings(expires: DateTime.Today.AddDays(7)); 
         SharedLinkMetadata sharedLinkMetadata = await dbx.Sharing.CreateSharedLinkWithSettingsAsync(remotePath, settings2); 
         return sharedLinkMetadata.Url; 
        } 
       } 
       else 
       { 
        var settings = new SharedLinkSettings(expires: DateTime.Today.AddDays(7)); 
        SharedLinkMetadata sharedLinkMetadata = await dbx.Sharing.CreateSharedLinkWithSettingsAsync(fileMetadata.PathLower, settings); 
        return sharedLinkMetadata.Url; 
       } 
      } 
      else 
      { 
       await this.ChunkUpload(dbx, remotePath, fileStream, ChunkSize); 
      } 
      return "error"; 
     } 
    } 

但它不能正常工作,當它到達的ListSharedLinksAsync功能,它停止不用其他拋出任何錯誤的工作。

我注意到,我嘗試上傳的文件是不可訪問後崩潰,我得到一個「由另一個proccess錯誤」 ......

我在做什麼錯?

+1

你是不是該行缺少的await? 'var sharedLink = dbx.Sharing.ListSharedLinksAsync(remotePath);''' – JOSEFtw

+0

您使用的是哪個框架版本? – dadde

+0

@JOSEFtw好點,我會試試。 – user3378165

回答

2

看起來你錯過了這一行的等待,從而導致死鎖?

var sharedLink = dbx.Sharing.ListSharedLinksAsync(remotePath);

應該

var sharedLink = await dbx.Sharing.ListSharedLinksAsync(remotePath);