1

このスニペットは、私のビジネス ロジック レイヤー クラス ファイルにあります。

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を使用しています。SocialAuth.NETすべてをセッションに保存します。たとえば、私たちが呼び出す Facebook ユーザー ID を取得するSocialAuthUser.GetCurrentUser.GetProfile.IDには、セッション ベースであるため、 Web サービス (asmx.vb) ファイルから呼び出そうとすると、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」というエラー メッセージが表示されます。SocialAuthUser.GetCurrentUser.GetProfile.ID私はこのように呼んでいます(ClassName.FunctionName-->BLL.getAccIDFromSocialAuthSession)

ページから同じ関数を呼び出すと、正常に機能しますが、aspx.vbページから呼び出すと機能しませんasmx.vb

セッション変数名がわからないので使えませんSystem.Web.HttpContext.Current.Session("NameHere")

解決策はありますか??

4

2 に答える 2

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.
于 2012-11-29T19:14:36.597 に答える
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

アップデート

あなたのウェブサービスではうまくいかないかもしれませんが、明確にすることはできませんでした。Web サービスはステートレスです。つまり、実際の Web サイトから独立して実行されます。そのため、インスタンス化された場所でSocialAuthUserは、Web サーバーはそれについて何もしません。独自のスタンドアロンのコードを実行するためにあります。

別のアップデート

この場合、Web サービスを使用するのではなく、.aspx ページの分離コードで Web メソッドを使用してみてください。

だから、このようなもの:

     <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
于 2012-11-29T17:36:23.827 に答える