使用LinqToTwitter搜索功能的NullReferenceException

时间:2015-01-14 09:25:40

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

我在我的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 }

执行搜索并将其放入var searchResponse时会发生错误。我已经验证身份验证已成功并且已设置bearertoken。除了Joe Mayo自己在LINQ to Twitter Project site on Codeplex提供的简单示例之外,没有其他任何花哨的东西。

我还尝试了一些不使用LinqToTwitter的工作(使用相同的凭据)直到收到推文列表,但由于序列化原因,我选择了LinqToTwitter。

感觉就像我在这里遗漏了一些明显的东西,比如在某个地方设置一些令牌或授权,但找不到它。来自codeplex的源文件包含的演示控制台项目似乎完美无缺。这里有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

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

这个问题从未真正解决过。我现在正在使用v3.1.2,并且还遇到了无法恢复的软件包问题(注意:只有Mac上的Xamarin Studio才会出现区分大小写)。但是,由于NuGet,我遇到了this topic遇到类似问题的用户。我手动编辑了packages.config(在PCL和iOS项目中)以声明id =&#34; linqtotwitter&#34;而不是&#34; LinqToTwitter&#34;这向我展示了我希望的魔力......

希望这会有所帮助......

答案 1 :(得分:-1)

我将您的搜索代码复制到我自己的应用程序中并且执行正常。我使用的是SingleUserAuthorizer而不是ApplicationOnlyAuthorizer,但在您的代码和我的代码中唯一不同的是我:

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

希望有所帮助