1

I've recently been using LinqToTwitter have incorporated it successfully into a couple of sites. One site is giving me a headache though. It appears to authenticate but as soon as you try and iterate over the collection of tweets returned I get an error:

"An existing connection was forcibly closed by the remote host"

Here is my code:

protected void Page_Load(object sender, EventArgs e)
    {
        var auth = new SingleUserAuthorizer
        {
            CredentialStore = new SingleUserInMemoryCredentialStore
            {
                ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["TwitterAPIKey"],
                ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["TwitterSecretKey"],
                AccessToken = System.Configuration.ConfigurationManager.AppSettings["TwitterAccessToken"],
                AccessTokenSecret = System.Configuration.ConfigurationManager.AppSettings["TwitterSecretToken"]
            }
        };

        var ctx = new TwitterContext(auth);
        var mytweets = ctx.Status.Where(t => t.Type == StatusType.User && t.ScreenName == "paulpitchford");

        try
        {
            if (mytweets != null)
            {
                foreach (var item in mytweets)
                {
                    tweets.TweetList.Add(TweetParser.ParseTweet(item.Text));
                }
            }
        }
        catch (Exception err)
        {
        }
    }

The error occurs as soon as it hits this line: foreach (var item in mytweets)

The keys match perfectly to the API given keys and I can't see that I'm doing much wrong. Can anyone suggest where I may be going wrong?

Thank you.

Paul.

paulpitchford
  • 550
  • 5
  • 24

1 Answers1

1

LINQ to Twitter is async. This means you need to materialize your query before going into the foreach loop, like this:

   var mytweets =
       await
       ctx.Status.Where(
           t => t.Type == StatusType.User && 
           t.ScreenName == "paulpitchford")
       .ToListAsync();

Notice the await on the query and the ToListAsync operator. Also, you must add the async modifier to Page_Load, like this:

    protected async void Page_Load(object sender, EventArgs e)
Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
  • Of course! I knew I was missing something. Thanks for your help. – paulpitchford Oct 17 '14 at 17:00
  • Hi, I never got this working and left it but now I'm revisiting it, I posted on http://linqtotwitter.codeplex.com/discussions/588450 fyi. I changed it all to asyn with no avail unfortunately. – paulpitchford Mar 22 '15 at 08:28