我正在尝试使用新的linq2twitter版本(4.1.0),但我无法让它工作。 到目前为止,我曾经使用过版本2.1.11,它运行得很好。
我已经开了一个新的asp.net项目,这是我的代码
你能告诉我我做错了什么吗? “DoSingleUserAuth”工作正常。我输入了正确的代币......
protected void Page_Load(object sender, EventArgs e)
{
Task demoTask = DoDemosAsync();
demoTask.Wait();
}
static async Task DoDemosAsync()
{
var auth = DoSingleUserAuth();
var twitterCtx = new TwitterContext(auth);
await ShowFriendsAsync(twitterCtx);
}
static IAuthorizer DoSingleUserAuth()
{
var auth = new SingleUserAuthorizer
{
CredentialStore = new SingleUserInMemoryCredentialStore
{
ConsumerKey = "ConsumerKey",
ConsumerSecret = "ConsumerSecret",
AccessToken = "AccessToken",
AccessTokenSecret = "AccessTokenSecret"
}
};
return auth;
}
static async Task ShowFriendsAsync(TwitterContext twitterCtx)
{
var friendship =
await
(from friend in twitterCtx.Friendship
where friend.Type == FriendshipType.Show &&
friend.SourceScreenName == "Linq2Twitr" &&
friend.TargetScreenName == "JoeMayo"
select friend)
.SingleOrDefaultAsync();
if (friendship != null &&
friendship.SourceRelationship != null &&
friendship.TargetRelationship != null)
{
Console.WriteLine(
"\nJoeMayo follows LinqToTweeter: " +
friendship.SourceRelationship.FollowedBy +
"\nLinqToTweeter follows JoeMayo: " +
friendship.TargetRelationship.FollowedBy);
}
}
10倍, 利奥尔
答案 0 :(得分:0)
你可以做两件事来解决这个问题,让你的页面异步并用等待替换等待:
将Async =“true”属性添加到@Page指令:
<%@ Page Async="true" ... %>
您可以通过将其设置为异步并等待DoDemosAsync来重写Page_Load:
protected async void Page_Load(object sender, EventArgs e)
{
await DoDemosAsync();
}
问题是调用Wait()导致死锁。使用异步,您应该在调用链中一直调用异步。
答案 1 :(得分:0)
我可以看到 var result = Task.Run(()=&gt; ShowFriendsAsync(twitterCtx))。结果;
对我来说很好。这是一个好方法吗?
利奥尔