【问题标题】:NullReferenceException using LinqToTwitter Search functionalityNullReferenceException 使用 LinqToTwitter 搜索功能
【发布时间】:2015-04-28 19:35:05
【问题描述】:

我在我的 Xamarin.IOS 统一应用程序中使用来自 Nuget 的 LinqToTwitter v3.1.1 和仅应用程序身份验证,以使用这个不错的库的搜索选项显示来自 TwitterAccount 的推文列表。但是,在执行简单搜索时,我在 TwitterQueryProvider 上收到未处理的 NullReference 异常。

我写的代码如下:

var credentialStore = new InMemoryCredentialStore
{
    ConsumerKey = "MyTwitterKey",
    ConsumerSecret = "MyTwitterSecret"
};

var authorizer = new ApplicationOnlyAuthorizer { CredentialStore = credentialStore };
await authorizer.AuthorizeAsync();

var twitterCtx = new TwitterContext(authorizer);

var searchResponse = await (from search in twitterCtx.Search
                           where search.Type == SearchType.Search 
                              && search.Query == "Microsoft"
                          select search)
                                 .SingleOrDefaultAsync();

if (searchResponse != null && searchResponse.Statuses != null)
{
    foreach (var tweet in searchResponse.Statuses)
    {
        Debug.WriteLine("User: {0}, Tweet: {1}", tweet.User.ScreenNameResponse, tweet.Text);
    }
}

下面的部分堆栈跟踪:

{System.NullReferenceException: Object reference not set to an instance of an object 
 at LinqToTwitter.TwitterQueryProvider+<ExecuteAsync>d__6`1[System.Collections.Generic.IEnumerable`1[LinqToTwitter.Search]].MoveNext () [0x00000] in <filename unknown>:0
 --- End of stack trace from previous location where exception was thrown --- 

 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in [somepath]/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Object].GetResult () [0x00034] in [somepath]/System.Runtime.CompilerServices/ConfiguredTaskAwaitable_T.cs:62 
 at LinqToTwitter.TwitterExtensions+<ToListAsync>d__11`1[LinqToTwitter.Search].MoveNext () [0x00000] in <filename unknown>:0 
 --- End of stack trace from previous location where exception was thrown --- 

 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in [somepath]/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
 at System.Runtime.CompilerServices.TaskAwaiter`1[System.Collections.Generic.List`1[LinqToTwitter.Search]].GetResult () [0x00034] in [somepath]/System.Runtime.CompilerServices/TaskAwaiter_T.cs:59 
 at [somepath].TwitterService+<GetTweets>d__2.MoveNext () [0x0014f] in [somepath]\TwitterService.cs:27 }

执行 Search 并将其放入 var searchResponse 时会发生错误。我已验证身份验证已成功并且已设置承载令牌。除了乔·梅奥本人在LINQ to Twitter Project site on Codeplex 上提供的简单示例之外,没有做过任何花哨的事情。

我也尝试了一些不使用 LinqToTwitter 的方法,该方法可以(使用相同的凭据)接收推文列表,但由于序列化原因,我选择使用 LinqToTwitter。

感觉好像我在这里遗漏了一些明显的东西,比如设置一些令牌或在某处授权,但找不到它。包含在 codeplex 源文件中的演示控制台项目似乎工作得很好。这里有什么想法吗?

【问题讨论】:

  • 有什么,有人吗?仍然有效!

标签: c# xamarin.ios xamarin linq-to-twitter


【解决方案1】:

出于历史目的,我欠你们一个可能的解决方案的答案。

简而言之:手动编辑 NuGet 的 packages.config 以为 linqtotwitter 设置小写 id 似乎可以解决我所有的问题。

问题永远无法真正解决。我现在正在使用 v3.1.2,并且还遇到了无法恢复的包问题(注意:仅在 Mac 上的 Xamarin Studio 上发生,它区分大小写)。但是,由于 NuGet,我遇到了this topic 的用户,他们遇到了类似的问题。我手动编辑了 packages.config(在 PCL 和 iOS 项目中)以声明 id="linqtotwitter" 而不是 "LinqToTwitter" 这向我展示了我希望的魔法......

希望这会有所帮助...

【讨论】:

    【解决方案2】:

    我将您的搜索代码复制到我自己的应用程序中,并且运行良好。我使用的是SingleUserAuthorizer 而不是ApplicationOnlyAuthorizer,但你的代码和我的唯一区别是我:

    • 包括accessTokenaccessTokenSecret
    • 我没有电话:await authorizer.AuthorizeAsync();

    希望对你有所帮助

    【讨论】:

    • 感谢您的评论,但是由于我在应用程序中使用了这个特殊的 ApplicationOnlyAuthorizer,所以创建一个幽灵用户对我来说毫无用处......
    猜你喜欢
    • 2020-01-16
    • 1970-01-01
    • 2017-03-21
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2017-03-15
    相关资源
    最近更新 更多