【问题标题】:Function not working with webservice功能不适用于网络服务
【发布时间】:2012-11-29 17:31:40
【问题描述】:

这个 sn-p 在我的业务逻辑层类文件中:

Public Shared Function getAccIDFromSocialAuthSession() As Object
        Dim db As SqlDatabase = Connection.connection
        Dim accID As Integer
        If SocialAuthUser.IsLoggedIn Then
            Using cmd As SqlCommand = db.GetSqlStringCommand("SELECT AccountID FROM UserAccounts WHERE FBID=@fbID")
                db.AddInParameter(cmd, "fbID", SqlDbType.VarChar, SocialAuthUser.GetCurrentUser.GetProfile.ID)
                accID = db.ExecuteScalar(cmd)
            End Using
        End If

        Return accID
    End Function

我正在使用SocialAuth.NetSocialAuth.NET 将所有内容存储在会话中。例如,要获取我们调用 SocialAuthUser.GetCurrentUser.GetProfile.ID 的 Facebook 用户 ID,因为它是基于会话的,所以当我尝试调用 @987654325 时,我收到此错误消息 "Object Reference Not Set to an instance of an object" @ 来自 web 服务 (asmx.vb) 文件。我这样称呼它(ClassName.FunctionName-->BLL.getAccIDFromSocialAuthSession)

当我从aspx.vb 页面调用相同的函数时,它可以正常工作,但当我从asmx.vb 页面调用它时就不行了。

由于我不知道会话变量名称,我不能使用System.Web.HttpContext.Current.Session("NameHere")

有什么办法吗??

【问题讨论】:

    标签: c# asp.net vb.net socialauth


    【解决方案1】:

    这是一些工作代码的开始:

    Imports System.Web.Services
    Imports System.ComponentModel
    
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class LightboxService
        Inherits WebService
    
        <WebMethod(EnableSession:=True)> _
        Public Function AddToLightbox(ByVal filename As String) As String
            Dim user As String = CStr(Session("username"))
    
    '...etc.
    

    【讨论】:

    • 工作就像一个魅力。我欠你一品脱。将其添加到您的列表中!谢谢男人。
    • +1 - 我忘记了 webmethod 调用中的 EnableSession:=True 。很好的收获。
    【解决方案2】:

    听起来你需要在你的服务类上实现Implements IRequiresSessionState

    没有这个,再多的调用HttpContext.Current.Session("NameHere") 都行不通。

    示例:(伪代码)

        <WebService(Namespace:="http://tempuri.org/")> _
        <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> 
        <System.Web.Script.Services.ScriptService()>
        Public Class MyService
                Inherits System.Web.Services.WebService
                Implements IRequiresSessionState '' <-- This is the part you need
    
                '' Code in here
        End Class
    

    更新

    我无法澄清,尽管它可能不适用于您的网络服务。 Web 服务是无状态的。这意味着它独立于您的实际网站运行。因此,无论何时 SocialAuthUser 已被实例化,网络服务器都不会对此一无所知。它可以运行它自己的独立代码。

    另一个更新

    在这种情况下,不要使用网络服务,而是尝试在您的 .aspx 页面代码隐藏上使用网络方法。

    所以是这样的:

         <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <Services.WebMethod(EnableSession:=True)> _
         Public Shared Function getAccIDFromSocialAuthSession() As Object
              Dim db As SqlDatabase = Connection.connection
              Dim accID As Integer
              If SocialAuthUser.IsLoggedIn Then
                  Using cmd As SqlCommand = db.GetSqlStringCommand("SELECT AccountID FROM UserAccounts WHERE FBID=@fbID")
                      db.AddInParameter(cmd, "fbID", SqlDbType.VarChar, SocialAuthUser.GetCurrentUser.GetProfile.ID)
                      accID = db.ExecuteScalar(cmd)
                  End Using
              End If
    
            Return accID
        End Function
    

    【讨论】:

    • 在调试器中你能看到来自SocialAuthUser.GetCurrentUser.GetProfile.ID的对象实际上是nothing。 IE 是 ID 还是 SocialAuthUser
    • 我尝试使用断点进行调试..SocialAuthUser 什么都没有。
    • 更大的问题是为什么要把它放在网络服务中?
    • getAccIDFromSocialAuthSession() 函数在类文件中..我只是想从网络服务中调用它..无论如何你有什么建议?
    • 我问“为什么?”的原因因为 web 服务是,如果你不从任何 AJAX 调用它或试图将你的方法暴露给外界,那么不要使用 web 服务。创建您自己的类,而不是可以在网站周围使用。
    猜你喜欢
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多