1

This snippet is in my Business Logic Layer Class File:

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

I am using SocialAuth.Net. SocialAuth.NET stores everything in session. For e.g to get the Facebook User ID we call SocialAuthUser.GetCurrentUser.GetProfile.ID , since it is session based i get this error message "Object Reference Not Set to an instance of an object" when i try to call SocialAuthUser.GetCurrentUser.GetProfile.ID from a webservice (asmx.vb) file. I am calling it like this (ClassName.FunctionName-->BLL.getAccIDFromSocialAuthSession)

Whaen i call the same function from a aspx.vb page it works fine but not when i call it from an asmx.vb page.

Since i do not know the session variable name , i cannot use System.Web.HttpContext.Current.Session("NameHere")

Any solution??

Monodeep
  • 1,392
  • 1
  • 17
  • 39

2 Answers2

2

Here's the start of some working code:

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.
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

It sounds like you need to implement Implements IRequiresSessionState on your service class.

Without this, no amount of calling HttpContext.Current.Session("NameHere") will work.

Example: (Pseudo-code)

    <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

UPDATE

I haven't been able to clarify, though it might just not work with your webservice. Web services are stateless. Meaning it runs independently from your actual web site. So where ever SocialAuthUser has been instantiated the webserver will no nothing about it. It's there to run it's own stand-alone piece of code.

Another Update

Rather than use a web service for this case, try using a webmethod on your .aspx page codebehind.

So something like this:

     <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
Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
  • In debugger can you see which object is actually `nothing` from `SocialAuthUser.GetCurrentUser.GetProfile.ID`. I.E is it `ID` or `SocialAuthUser` etc – Darren Wainwright Nov 29 '12 at 18:18
  • I tried to debug using break points..`SocialAuthUser` is Nothing. – Monodeep Nov 29 '12 at 18:30
  • The bigger question is why put it in a web service? – Darren Wainwright Nov 29 '12 at 19:06
  • `getAccIDFromSocialAuthSession()` fuction is in a class file..i am just trying to call it from a web service..anyway what do you suggest? – Monodeep Nov 29 '12 at 19:08
  • Reason i asked 'Why?' as webservice is that if you're not calling it from any AJAX or trying to expose your methods to the outside world then don't use the web service. Create your own classes instead that can be used around the site. – Darren Wainwright Nov 29 '12 at 19:11
  • I am calling it from an AJAX function. – Monodeep Nov 29 '12 at 19:12
  • Ok - updating my answer - give me 2 mins – Darren Wainwright Nov 29 '12 at 19:12
  • Your latest update would definitely work, but i call the same code from multiple places, so i would have to write the same code in almost every page.Anyway, check Andrew Morton's answer. It solved my issue. Thanks a lot for the effort and time you put into this. – Monodeep Nov 29 '12 at 19:26