其他分享
首页 > 其他分享> > CodeGo.net>如何从ExecuteAsync()方法获取字段

CodeGo.net>如何从ExecuteAsync()方法获取字段

作者:互联网

我熟悉在Google云端硬盘中创建文件夹的方法,但是我正在考虑使用异步方法来完成此操作.但是,这样做时,我不确定如何获取明确添加到要返回的字段中的字段.

我的代码如下:

private Task<Google.Apis.Drive.v3.Data.File> CreateGoogleDriveFolderAsync(DriveService service, string foldername, string parent_id = null)
{
    IList<string> parent_ids = new List<string>();

    Google.Apis.Drive.v3.Data.File folder = new Google.Apis.Drive.v3.Data.File
    {
        Name = foldername
        , MimeType = "application/vnd.google-apps.folder"
        , Description = "Client Name: blah\nUser: Rudy\n"
    };

    var insert = service.Files.Create(folder);

    // The field I'd like to get somewhere.
    insert.Fields = "id";

    var task = insert.ExecuteAsync();

    task.ContinueWith(t =>
    {
        // NotOnRanToCompletion - this code will be called if the upload fails
        Console.WriteLine("Failed to create folder \"{0}\": " + t.Exception, foldername);
    }, TaskContinuationOptions.NotOnRanToCompletion);
    task.ContinueWith(t =>
    {
        // I'd like a way to access "id" from my insert execution.
        log.insertLogging(foldername, "Directory Created");
    });

    return task;
}

解决方法:

documentation为例,等待任务并从那里获取文件,您应该可以访问其属性

private async Task<File> CreateGoogleDriveFolderAsync(DriveService driveService, string foldername) {
    var metadata = new File()
    {
        Name = foldername,
        MimeType = "application/vnd.google-apps.folder"
    };
    var request = driveService.Files.Create(metadata);
    request.Fields = "id";
    var folder = await request.ExecuteAsync();
    Console.WriteLine("Folder ID: " + folder.Id);

    return folder;
}

标签:asynchronous,async-await,google-drive-api,c
来源: https://codeday.me/bug/20191026/1933947.html