如果需要在Client和Server端保持状态信息,则可以使用EnableSession属性。缺省情况下EnableSession为false。
WebService端代码:
Public Property Connect() As String
<WebMethod(EnableSession:=True)> Get
Return Session("_str")
End Get
<WebMethod(EnableSession:=True)> Set(ByVal Value As String)
Session("_str") = Value
End Set
End Property
Public Property SQL() As String
<WebMethod(EnableSession:=True)> Get
Return Session("_str1")
End Get
<WebMethod(EnableSession:=True)> Set(ByVal Value As String)
Session("_str1") = Value
End Set
End Property
然后在VB.NET的Applicaton中可以安装如下方式调用上述Web Service:
如下只是一个简单的测试代码,可供参考:
在Button2_Click方法中可以第二次获取Session("_str")的值,不需要重新再赋值。
Imports System.Net
Dim ws As localhost.HelloWorld = New localhost.HelloWorld()
……
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ws.CookieContainer = New CookieContainer()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strTemp As String
ws.set_Connect("Connected!")
strTemp = ws.get_Connect()
TextBox1.Text = TextBox1.Text & strTemp
ws.set_SQL("SQL")
strTemp = ws.get_SQL()
TextBox1.Text = TextBox1.Text & Chr(13) & Chr(10) & strTemp
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text & ws.get_Connect()
End Sub