通过 LinqToTwitter 上传媒体在 WebForms 中挂起,在 ConsoleApp 中工作正常

Uploading media via LinqToTwitter hangs in WebForms, works fine in ConsoleApp

我在使用 LinqToTwitter 上传视频时遇到问题。我有一个示例代码,在 Console application 中调用时工作正常,但在 WebForms 项目中使用时挂起。

private static ulong UploadMedia()
{
    const string path = "c:\Temp\video.mp4";
    var authorizer = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = "my_consumer_key",
            ConsumerSecret = "my_consumer_secret",
            AccessToken = "my_access_token",
            AccessTokenSecret = "my_access_token_secret"
        }
    };
    var tc = new TwitterContext(authorizer);
    var media = UploadMediaAsync(tc, File.ReadAllBytes(path)).Result;
    return media.MediaID;
}

private static async Task<Media> UploadMediaAsync(TwitterContext tc, byte[] media)
{
    return await tc.UploadMediaAsync(media, "video/mp4");
}

有人知道哪里出了问题吗?

您通过调用 .Result

阻塞了 WebForms UI 线程

请参阅 http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html 以了解问题及其解决方法。