【问题标题】:Program not responding from SearchListResponse程序没有从 SearchListResponse 响应
【发布时间】:2018-09-17 11:40:08
【问题描述】:

我正在尝试使用 Winforms 应用收集 YouTube 视频的数据。当我如下所示调用YTSearch() 方法时,程序在到达时停止响应

var slResponse = await slRequest.ExecuteAsync(); 

请求已启动,但并未停止,也未完成,也未在 try catch 上触发 catch。我已经设法使用 Discord 机器人,使用 Discord.NET 来实现完全相同的工作。在输出中是

抛出异常:Google.Apis.dll 中的“Google.GoogleApiException”
抛出异常:mscorlib.dll 中的“Google.GoogleApiException”
抛出异常:mscorlib.dll 中的“Google.GoogleApiException”

库:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.YouTube.v3;
using Google.Apis.Services;
using System.Diagnostics;

方法:

public async Task<List<Video>> YTSearch(string query)
{
    string ytAPI = APIKEY; // Ommitted
    var ytService = new YouTubeService(new BaseClientService.Initializer()
    {
        ApiKey = ytAPI,
        ApplicationName = "YouTubeDownloader"
    });
    var slRequest = ytService.Search.List("snipper");
    slRequest.Q = query;
    slRequest.MaxResults = 10;
    var slResponse = await slRequest.ExecuteAsync();
    List<Video> vidList = new List<Video>();
    return vidList;
}

致电:

private void btnSearch_Click(object sender, EventArgs e)
{
    List<Video> vidList = YTSearch(txtSearch.Text).Result;
}

【问题讨论】:

    标签: c# google-api youtube-api youtube-data-api google-api-dotnet-client


    【解决方案1】:

    我尚未对此进行测试,但我相当确定您需要使用 ConfigureAwait(false)。因此,在您的 YTSearch 方法中,您需要使用以下行:

    var slResponse = await slRequest.ExecuteAsync().ConfigureAwait(false);
    

    这是必需的,因为您正在使用 .Result 阻止按钮处理程序中的结果。

    This MSDN article 有点老了,但说明了问题。

    【讨论】:

    • 我现在试试
    【解决方案2】:

    首先,您应该修复一个与您的错误无关的小错字。第一个参数是部分有效值是

    1. 内容详情:2
    2. id:0
    3. sn-p: 2

    你有狙击手

    目前尚不清楚这是问题所在,因为您遇到的错误与 API 无关。

    这个问题听起来更像是 dll 的问题。我会尝试重新导入 nuget 包,确保您在项目中使用.net 4.5

    【讨论】:

    • 我现在不在我的电脑上,但我认为这实际上可能是错误。我现在去看看
    • 好吧,sniper 不是错误,我使用的是 .NET 4.6.1。我已经重新导入了 nuget 包,但仍然收到错误。
    • 今天晚些时候我会启动我的电脑并测试它
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多