C# 函数不使用webservice

C# 函数不使用webservice,c#,asp.net,vb.net,socialauth,C#,Asp.net,Vb.net,Socialauth,此代码段位于我的业务逻辑层类文件中: 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.

此代码段位于我的业务逻辑层类文件中:

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.NET
在会话中存储所有内容。例如,为了获取Facebook用户ID,我们调用了
SocialAuthUser.GetCurrentUser.GetProfile.ID
,因为它是基于会话的,所以当我试图从webservice(asmx.vb)文件调用
SocialAuthUser.GetCurrentUser.GetProfile.ID
时,我会收到以下错误消息“对象引用未设置为对象的实例”。我这样叫它(ClassName.FunctionName-->BLL.getAccIDFromSocialAuthSession)

当我从
aspx.vb
页面调用同一个函数时,它工作正常,但当我从
asmx.vb
页面调用它时,它就不工作了

由于我不知道会话变量名,因此无法使用
System.Web.HttpContext.Current.session(“NameHere”)


任何解决方案???

听起来您需要在服务类上实现
实现iRequiresessionState

如果没有这一点,调用HttpContext.Current.Session(“NameHere”)的次数将无法正常工作

示例:(伪代码)


下面是一些工作代码的开始:

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.
导入System.Web.Services
导入System.ComponentModel
_
_
_
_
公共级LightboxService
继承Web服务
_
公共函数AddToLightbox(ByVal文件名为字符串)为字符串
Dim user As String=CStr(会话(“用户名”))
“……等等。

在调试器中,您可以从
SocialAuthUser.GetCurrentUser.GetProfile.ID
中看到哪个对象实际上是
nothing
。也就是说,它是
ID
还是
SocialAuthUser
etcI试图使用断点进行调试。
SocialAuthUser
什么都不是。更大的问题是为什么要将它放在web服务中?
getAccIDFromSocialAuthSession()
fuction在一个类文件中。我只是试图从web服务调用它。不管怎样,你有什么建议?我问“为什么”的原因是,如果你不是从任何AJAX调用它,或者试图向外界公开你的方法,那么就不要使用web服务。创建你自己的类,可以在网站上使用。工作起来很有魅力。我欠你一品脱。将它添加到您的列表中!!谢谢你,+1-我忘记了webmethod调用中的EnableSession:=True。抢手货
     <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
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.