Web Services用session

asp.net Web Services默认状态下是禁用session的,要用session,要设置一下。

 

1、VB.NET的方式

<WebMethod(EnableSession:=True)> _

    Public Function HelloWorld() As String
        Try
            Dim strUrlSTD As String
            Session("ss") = "aaa"

            strUrlSTD = "GenStdChartProgressUI.aspx?ss=" + Session("ss")

            Return strUrlSTD
            'Return "Hello World"
        Catch ex As Exception
            Return ex.Message.ToString
        End Try
    End Function

 

2、c#

[   WebMethod(EnableSession=true)]  

 

 

凡是有使用微軟ASP或ASP.NET開發過web應用程式經驗的人都知道,藉session(工作階段)物件可以來維護狀態。那麼,ASP.NET Web Services是不是也可以使用Session來維護狀態呢? 答案是肯定的。這篇文章就是要簡單的跟大家介紹在ASP.NET Web Services中如何來設定使用Session(HttpSessionState),以及ASP.NET Web Services的用戶端該如何才能讓此Session正常運作來維護狀態。

        ASP.NET Web Services預設並未支援Session,所以在Web Services程式碼中不能直接使用Session,如果要使用,則必須先做一些設定。首先請確認web.config組態設定檔當中<sessionState>的mode屬性值不是Off,如果是Off,則Session不會運作,後續的其他設定也不會有作用。其次須設定Web Services物件類別繼承System.Web.Services.WebService類別。繼承此類別讓Web Services類別可以直接使用ASP.NET物件,如Session、Application等。最後,在方法宣告的< WebMethod ()>屬性 (Attribute)中加入(EnableSession:=True)即可。

        下面是一個ASP.NET Web Service使用Session的簡單例子。在這個例子中,如果Session發揮維護狀態的效果,則同一使用者每呼叫一次Web Service的Counter方法,回傳值會以加一的方式遞增。

<WebService(Namespace:="http://127.0.0.1/webServices/")> _
Public Class sessionService
           Inherits System.Web.Services.WebService

        <WebMethod(EnableSession:=True)> _
        Public Function Counter() As Integer
            Dim count As Integer
            If Session("Count") Is Nothing Then
                count = 1
            Else
                count = Session("Count") + 1
            End If
            Session("Count") = count
            Return count
        End Function
      End Class

        通常在完成了上面一個Web Service後,開發者會在IE瀏覽器中進行測試,結果確實可以得到預期中Session發揮了維護狀態的效果。但是,Web Services並不是開發來讓人用瀏覽器瀏覽的,而是由用戶端程式來呼叫使用的。在用戶端程式中叫用上面這個Web Service時,Session是否還會發揮效果呢? 答案還是肯定的,但是它卻不像用瀏覽器叫用一般的簡單直接,而是有些事情必須注意的。

        下面是一個呼叫上面這個Web Service的簡單範例。 做法是透過在.NET Framework裡建立呼叫Web Service的Proxy代理程式(WsNamespace.sessionService)來呼叫。

      Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
            Dim ws As New WsNamespace.sessionService()
            Dim count As Integer
            count = ws.Counter()
            Label1.Text = count
      End Sub

        Session的運作預設是使用Cookie來存放和傳遞辨識用戶端的SessionID,如果沒有Cookie的配合,則依預設的做法,Web Services無法透過SessionID來辨識使用者,如此便造成Session無法運作。在IE瀏覽器中進行測試Web Services時,通常瀏覽器都會支援Cookie,也因此Session可以正常發揮效果,但在用戶端程式中呼叫Web Services時,則必須在用戶端程式中利用CookieContainer來存放Cookie,如此才能傳遞SessionID來支援Session的運作。下面的例子就是改寫前面的範例,在代理程式中設定CookieContainer。

‘產生cookieContainer物件實例
Private CookieCnt As New System.Net.CookieContainer()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
        Dim ws As New WsNamespace.sessionService()

        ‘將CookieContainer物件實例設給呼叫Web Services的Proxy來存放Cookie
        ws.CookieContainer = CookieCnt
        Dim count As Integer
        count = ws.Counter()
        Label1.Text = count
    End Sub
執行這個範例將可得到預期中count值依次加一的結果。

        以上簡單的介紹了在ASP.NET Web Services 中使用Session的做法以及用戶端程式該注意的地方,希望對大家有所幫助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值