LinqtoTwitter TweetAsync never returns in ServiceStack App
-
21-12-2019 - |
Question
When using LinqToTwitter the status update gets posted to twitter with no problems, however the route times out and when debugging the if statement after the await line the debugger never reaches that point. What can I do to fix this?
async public Task<object> Post(RetweetPost retweetInfo){
...
var auth = new SingleUserAuthorizer
{
CredentialStore = new SingleUserInMemoryCredentialStore
{
ConsumerKey = "ConsumerKey",
ConsumerSecret = "ConsumerSecret",
AccessToken = account.OAuthToken,
AccessTokenSecret = account.OAuthTokenSecret
}
};
var twitterCtx = new TwitterContext(auth);
var tweet = await twitterCtx.TweetAsync(retweetInfo.tweet);
//DOES NOT REACH THIS LINE
if (tweet != null)
{
return new RetweetPostResponse
{
twitterTweetId = tweet.StatusID.ToString()
};
}
Solution 2
I believe the real problem I was facing was the ServiceStack Framework. As discussed here. Apparently async functions on routes were not supported until ServiceStack 4.
OTHER TIPS
You probably have a call to Task<T>.Result
or Task.Wait
further up your call stack. This will cause a deadlock that I describe in detail on my blog.
await
will capture a "context" by default (e.g., a UI context) and use that to resume the async
method. When the TweetAsync
task completes, it attempts to resume within that context (e.g., on the UI thread), but it can't because there's already a thread blocked in that context (i.e., calling Result
/Wait
).
The best solution is to replace all Result
and Wait
calls with await
.