通过LinqToTwitter上传媒体在WebForms中挂起,在ConsoleApp中运行良好

时间:2016-10-14 10:10:59

标签: c# upload async-await linq-to-twitter

我使用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");
}

任何人都知道出了什么问题?

1 个答案:

答案 0 :(得分:1)

您正在通过调用rbind

来阻止WebForms UI线程

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