I have an issue where after making a query to twitter via the LinqToTwitter library from within a web api controller, my code stops executing and the web api action fails to return a response. I suspect that there is possibly some sort of conflict between web api and linqToTwitter, but I am totally clueless here. Here is the code:
// This class is my wrapper for linqToTwitter
public class Twitter
{
// FIELDS
private readonly MvcAuthorizer _auth;
// PROPERTIES
public TwitterContext Twit
{
get
{
return new TwitterContext(_auth);
}
}
public Twitter()
{
// all keys
var cKey = ConfigurationManager.AppSettings[
"twitConsumerKey"];
var cSecKey = ConfigurationManager.AppSettings[
"twitConsumerSecret"];
var oToken = AuthTool.getUserTwitterTokens().token;
var oTokenSecc = AuthTool.getUserTwitterTokens()
.tokenSecret;
ICredentialStore cred = new InMemoryCredentialStore();
cred.ConsumerKey = cKey;
cred.ConsumerSecret = cSecKey;
cred.OAuthToken = oToken;
cred.OAuthTokenSecret = oTokenSecc;
// get auth
_auth = new MvcAuthorizer()
{
CredentialStore = cred
};
}
// This is the specific method called from within the web api action
public string RetrieveTwitterId(string twitName)
{
// this statement executes a successful linqToTwitter query as verified with fiddler
// but doesn't return anything to the user variable. The rest of the code does not execute.
var user = Twit.User.SingleOrDefault
(x => x.Type == UserType.Show &&
x.ScreenName == twitName);
if(user != null)
{
return user.UserID.ToString("F0");
}
return null;
}
}
This is the web api action being called:
[HttpPost]
public HttpResponseMessage CreateNewInterview(Model.NewInterview interview)
{
// find twitter Id
var twitterId = new Twitter().RetrieveTwitterId(interview.SubjectTwitterName);// nothing is returned here and nothing executes below
// assign model
var model = new NewInterview
(interview.Title, twitterId, DateTime.Now, interview.PublicQuestions, interview.Category);
// create the new interview
Admin.createInterview(model);
// create response
return Request.CreateResponse(HttpStatusCode.OK);
}
I had these using statements in my twitter wrapper file:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;// <-- Had to remove this one!!!
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using LinqToTwitter;
using LinqToTwitter.Security;
using TwitterTools;
Caused an ambiguous error. Still don't fully understand why this was happening in my original code though.