繁体   English   中英

LinqToTwitter Async不起作用/'名称'await'在当前上下文中不存在'和其他麻烦

[英]LinqToTwitter Async not working / 'The name 'await' does not exist in the current context' and other pains

我一直在和Linq一起玩推特,经过很多痛苦,终于得到了一些回报。 该代码正在工作...

    public IList<ITweet> BuildForAuthor(string author)
    {
        var result = new List<ITweet>();

        context = context != null ? context : new TwitterContext(_auth);

        var tweets = (from tweet in context.Status
                      where tweet.Type == StatusType.User &&
                            tweet.ScreenName == author
                      select tweet).Take(4).ToList();

        if (tweets != null)
        {
            foreach (var tweet in tweets)
            {
                if (!tweet.Retweeted)
                {
                    result.Add(new Tweet
                    {
                        Author = tweet.ScreenName,
                        Text = tweet.Text
                    });
                }
                else
                {
                    result.Add(new Tweet
                    {
                        Author = tweet.RetweetedStatus.User.ScreenNameResponse,
                        Text = tweet.RetweetedStatus.Text,
                        Retweet = true,
                        RetweetedBy = tweet.ScreenName
                    });
                }
            }
        }

        return result;
    }

我的问题是它不是异步的(并且需要错误处理)。 我对异步有点不了解,因为它对我来说是全新的(使用JavaScript已有多年,因此可以理解这个概念)。 LinqToTwitter的文档坚称它必须是异步的,但是所有异步示例都无法为我运行。

这是完全错误的...

    public IList<ITweet> BuildForAuthor(string author)
    {
        var result = new List<ITweet>();

        context = context != null ? context : new TwitterContext(_auth);

            // This is the example as given in the LinqToTwitter documentation, but the 'await' command doesn't work - I see an error 'The name 'await' does not exist in the current context' - errr...
            var tweets = await (from tweet in context.Status
                                where tweet.Type == StatusType.User &&
                                      tweet.ScreenName == author
                                select tweet).ToListAsync();

            // This code is completely wrong - I'm not sure what to do here - In JS I'd be looking at a callback, but C# seems to just continue after from the examples I've looked at, however 'tweets' is no longer a list, it's a 'Task' so not much use to me here.
        if (tweets != null)
        {
            foreach (var tweet in tweets)
            {
                if (!tweet.Retweeted)
                {
                    result.Add(new Tweet
                    {
                        Author = tweet.ScreenName,
                        Text = tweet.Text
                    });
                }
                else
                {
                    result.Add(new Tweet
                    {
                        Author = tweet.RetweetedStatus.User.ScreenNameResponse,
                        Text = tweet.RetweetedStatus.Text,
                        Retweet = true,
                        RetweetedBy = tweet.ScreenName
                    });
                }
            }
        }

这里的任何建议都将受到欢迎。 我已经在Google文章和示例代码中搜寻了好几个小时,而当我查看无法从哪里开始的翻译时,这些示例与我的相距太远了-我认为这并不奇怪我收到的“等待”错误,所以我担心我在某处吠错了树。

第二天

经过更多的阅读后,我现在有了更多类似的东西...

            var tweets = (from tweet in context.Status
                      where tweet.Type == StatusType.User &&
                            tweet.ScreenName == author
                      select tweet).Take(4).ToListAsync();
        tweets.Wait();

        if (tweets != null)
        {
            foreach (var tweet in tweets.Result)
            {
                ...

(以上摘录自第二个代码块)-通过在tweets任务上手动调用Wait()方法,这似乎可以正常工作,但仍无法回答“ await”关键字为何无效的问题。

尽管我不确定是否令人满意,但最终我还是陷入了困境。

以上是我最终决定采用的解决方法。 我一直在考虑使异步工作贯穿始终。 我缺少的技巧是,为了使用await,包含await调用的方法本身必须是异步的。

如果我正在执行的方法调用是在控制器中,那会很好,但事实并非如此,它来自局部调用。 这将是一个重大变化。

为了使事情变得更加复杂,我基本上不希望页面在数据返回之前呈现,这样做会由于站点设计的性质而带来一些重大的前端挑战。

总结一下...

如果您实际上希望它异步,则需要从控制器方法返回数据。 如果您不这样做,那么我认为上述我的解决方法应该对您有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM