LinqToTwitter BeginAuthorizationAsync抛出NullReferenceException

时间:2015-06-16 16:49:53

标签: asp.net vb.net asp.net-mvc-4 linq-to-twitter

我正在尝试使用LinqToTwitter nuget插件(v3.1.2)将twitter集成添加到现有的MVC4应用程序中,并遵循文档here

Imports LinqToTwitter

Public Class OAuthController
Inherits System.Web.Mvc.Controller

Async Function BeginAsync() As Threading.Tasks.Task(Of ActionResult)
    Dim auth = New MvcAuthorizer() With { _
        .CredentialStore = New SessionStateCredentialStore() With { _
            .ConsumerKey = ConfigurationManager.AppSettings("consumerKey"), _
            .ConsumerSecret = ConfigurationManager.AppSettings("consumerSecret") _
        } _
    }

    Dim twitterCallbackUrl As String = Request.Url.ToString().Replace("Begin", "Complete")
    Dim callbackUrl = New Uri(twitterCallbackUrl)
    Return Await auth.BeginAuthorizationAsync(callbackUrl)
End Function

第二行到最后一行抛出System.NullReferenceException: Object reference not set to an instance of an object.异常,因为从插件中抛出错误我不确定如何继续。

堆栈追踪here

1 个答案:

答案 0 :(得分:1)

我发现了问题。根本原因是因为BeginAuthorizationAsync使用了隐藏的HttpContext.Current,这不是访问MVC控制器上下文的正确方法。

我在这里提交了一个拉取请求:https://github.com/JoeMayo/LinqToTwitter/pull/18

作为一种解决方法,您可以在调用BeginAuthorizationAsync之前添加以下代码。

auth.GoToTwitterAuthorization = (authUrl) => {
    ControllerContext.HttpContext.Response.Redirect(authUrl);
};