2012-11-29 34 views
1

這個片段的工作是我的業務邏輯層類文件:功能無法與web服務

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 [email protected]") 
       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存儲會話中的所有內容。例如,要獲得Facebook用戶ID,我們呼叫SocialAuthUser.GetCurrentUser.GetProfile.ID,因爲它是基於會話的,所以當我嘗試從web服務(asmx.vb)調用SocialAuthUser.GetCurrentUser.GetProfile.ID時,出現此錯誤消息「對象引用未設置爲對象的實例」文件。我打電話像這樣(ClassName.FunctionName - > BLL.getAccIDFromSocialAuthSession)

Whaen我調用同一個函數從aspx.vb頁面它工作正常,但不是當我把它從一個asmx.vb頁。

,因爲我不知道該會話變量名,我不能使用System.Web.HttpContext.Current.Session("NameHere")

任何解決方案?

回答

2

下面是一些工作代碼的開始:

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. 
+0

工作就像一個魅力。我欠你一杯。將它添加到你的清單!謝啦。 – Monodeep

+0

+1 - 我在網絡方法調用中忘記了EnableSession:= True。接得好。 – Darren

+0

@Monodeep不客氣。乾杯! –

0

聽起來你需要在你的服務類中實現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 

UPDATE

我一直無法澄清,儘管它可能只是不能與你的web服務工作。 Web服務是無狀態的。意思是它獨立於您的實際網站運行。所以SocialAuthUser已經實例化,網絡服務器將不會有任何關於它。它在那裏運行它自己的獨立代碼段。

不是使用這種情況下的網絡服務,而另一個更新,請嘗試使用您的.aspx頁面的代碼隱藏一個WebMethod。

因此,像這樣:

 <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 [email protected]") 
        db.AddInParameter(cmd, "fbID", SqlDbType.VarChar, SocialAuthUser.GetCurrentUser.GetProfile.ID) 
        accID = db.ExecuteScalar(cmd) 
       End Using 
      End If 

     Return accID 
    End Function 
+0

我試了一下,這是行不通的。 – Monodeep

+0

在調試器中,您可以從「SocialAuthUser.GetCurrentUser.GetProfile.ID」中看到哪個對象實際上是「Nothing」。 I.E是'ID'或'SocialAuthUser'等 – Darren

+0

我試着用斷點進行調試..'SocialAuthUser'是Nothing。 – Monodeep