我不确定这里发生了什么,它使用 Twitter API 进行身份验证就好了。
但是当它到了应该发布到Twitter的地步时,它会抛出一条StackOverflowException
,上面写着:
类型为"System.StackOverflowException"的未处理异常发生在姆科利布.dll
我很困惑。下面的代码是导致并最终导致异常的代码。
void StartValidation()
{
Console.Clear();
//Start Status thread
var status = TextAndUi.GetStatisThread();
status.Start("Validating");
//Check for Messages
var tweetAndSenderData = Imap.GetUnreadMessageAndSender();
if (tweetAndSenderData != null)
{
//Authurize connection and app
var authenticate = new Authenticate();
var tweetApp = authenticate.CreateClient();
//End thread
status.Abort();
Console.WriteLine("Validated!");
Console.Clear();
//Post tweets
PostContent("test", tweetApp);
//Delete messages
Imap.DeleteMessages();
}
else
{
//End thread
status.Abort();
TextAndUi.ShowSomethingToTheUser("The box is empty, or TTT could not secure a connection", true);
}
}
void PostContent(string myTweet, TwitterService tweetApp)
{
if (TextAndUi.MessageIsSuitableLength(myTweet))
{
PostTweet(tweetApp, myTweet);
}
}
void PostTweet(TwitterService tweetApp, string tweet )
{
var tweetOptions = new SendTweetOptions() {Status = tweet};
tweetApp.SendTweet(tweetOptions); /*The line that throws the exception*
}
正在使用的库是TweetSharp。
编辑:添加了调用堆栈数据
姆科利布.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) + 0x6b bytes Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x27 字节 姆科利布.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)+ 0x6f 字节 姆科利布.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0xa7 bytes mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x16 字节姆科利布.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x41 字节 姆科利布.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 字节
我遇到了同样的问题,并且能够解决它。对我来说,出现问题是因为在Twitter上,我只给了我的应用程序读取访问权限。若要从代码编写推文,应用程序必须在 Twitter 上注册为读/写。
为此,我必须首先从我正在使用它的Twitter帐户中撤销该应用程序。然后,我在dev.twitter上更改了应用程序,以便它可以读/写。然后,我生成了一个新的访问令牌,并能够从我的代码发送推文。
希望有帮助。
堆栈溢出通常意味着您的程序已经进入了递归调用的方法的无限循环。
最可能的原因是:
-
您正在使用的库中存在错误。由于您的代码很简单,因此库的作者似乎不太可能在测试中错过这样的错误,但您可能正在传入导致问题的参数值。您可以尝试使用不同的参数进行一些调用,以查看它是否与您调用它的方式有关。
-
您编写但未发布的某些代码导致对自身的递归调用。这在以下情况下可能非常明显:
void a() { a(); }
但也非常微妙 - 如果您尝试发布推文作为对事件的反应,则发送推文可能会导致事件再次引发,从而导致无限反馈循环。检查这一点的最简单方法是在 SendTweet() 行上放置一个断点,并检查断点是否多次命中。如果是,则需要消除重入调用 - 这可以通过在进行调用之前取消注册事件处理程序(并在之后再次重新注册)或使用变量来抑制对调用的处理来完成,如下所示:bool mSuppressTweets; void PostTweet(TwitterService tweetApp, string tweet ) { if (mSuppressTweets) return; var tweetOptions = new SendTweetOptions() {Status = tweet}; mSuppressTweets = true; tweetApp.SendTweet(tweetOptions); mSuppressTweets = false; }
如果这些都无济于事,请尝试隔离问题。创建一个新的"hello world"项目,只发送一条推文,没有别的。然后你会知道你有一个有效的推文解决方案,你可以将工作代码迁移到你的原始应用程序中,看看它是否在那里工作。如果它仍然不起作用,您就知道您的应用正在执行与"hello world"测试不同的操作。
我做了一个帐户来回答这个问题,我没有足够的"声誉"来评论,但 Greg B 的答案是正确的。
如果你的应用没有发布推文所需的权限,调用 SendTweet(SendTweetOptions) 方法将导致 StackOverflowException。
我刚刚通过在 https://dev.twitter.com/登录我的帐户并在设置下更新我的应用程序权限,然后重新验证我的帐户(即生成新令牌)来解决同样的问题。