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.
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)