我有一个问题,在通过LinqToTwitter库从web api控制器中查询twitter后,我的代码停止执行,web api动作未能返回响应。我怀疑web api和linqToTwitter之间可能存在某种冲突,但我在这里完全没有头绪。下面是代码:
// 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;
}
}
这是正在被调用的web api操作:
[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);
}
在我的twitter包装文件中有这些using语句:
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;
导致了一个模棱两可的错误。我仍然不完全明白为什么在我的原始代码中会发生这种情况。