Variables de sesión en WebServices[Como utilizar desde distintos tipos de clientes las variables de sesión de un WebService]
Fecha: 03/Nov/2005 (02-11-2005)
|
En este artículo veremos la forma de acceder desde distintos tipos de cliente (Windows Form y Web Form) a un WebService que posee variables de sesión sin que se pierda el contenido entre las distintas llamadas al mismo.
Primero desarrollaremos el WebService, modificando el atributo WebMethod con la propiedad EnableSession, para que el mismo permita utilizar variables de sesión.
Imports System.Web.Services <System.Web.Services.WebService(Namespace:="http:"//tempuri.org/SessionCountWS/SessionCount")> _ Public Class SessionCount Inherits System.Web.Services.WebService #Region " Web Services Designer Generated Code " Public Sub New() MyBase.New() 'This call is required by the Web Services Designer. InitializeComponent() 'Add your own initialization code after the InitializeComponent() call End Sub 'Required by the Web Services Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Web Services Designer 'It can be modified using the Web Services Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() components = New System.ComponentModel.Container End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 'CODEGEN: This procedure is required by the Web Services Designer 'Do not modify it using the code editor. If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub #End Region <WebMethod(EnableSession:=True)> _ Public Function Count() As Integer If Session.Item("SessionCount") Is Nothing Then Session.Item("SessionCount") = 1 Else Session.Item("SessionCount") = CInt(Session.Item("SessionCount")) + 1 End If Return CInt(Session.Item("SessionCount")) End Function End ClassAhora veremos como hacer desde el código del cliente para que al llamar al WebService mantenga la sesión.
Cliente Windows Form:
Primero debemos declarar un objeto del tipo System.Net.CookieContainer para luego asignárselo al WebService.
Private Cookies As System.Net.CookieContainerAhora declaramos el WebService, le asociamos las cookies al mismo y lo llamamos.
Private wsSessionCount As New SessionCountWS.SessionCount wsSessionCount.CookieContainer = Cookies MsgBox(wsSessionCount.Count.ToString)Con estos pasos, logramos mantener la sesión con que se llama al WebService en el cliente.
Veamos un ejemplo completo del mismo:
Public Class SessionCount Inherits System.Windows.Forms.Form Private Cookies As System.Net.CookieContainer Private wsSessionCount As New SessionCountWS.SessionCount #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call If Cookies Is Nothing Then Cookies = New System.Net.CookieContainer End If wsSessionCount.CookieContainer = Cookies End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBoxSessionCount As System.Windows.Forms.TextBox Friend WithEvents ButtonSessionCount As System.Windows.Forms.Button Friend WithEvents ButtonSessionCountGet As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBoxSessionCount = New System.Windows.Forms.TextBox Me.ButtonSessionCount = New System.Windows.Forms.Button Me.ButtonSessionCountGet = New System.Windows.Forms.Button Me.SuspendLayout() ' 'TextBoxSessionCount ' Me.TextBoxSessionCount.Location = New System.Drawing.Point(102, 65) Me.TextBoxSessionCount.Name = "TextBoxSessionCount" Me.TextBoxSessionCount.ReadOnly = True Me.TextBoxSessionCount.Size = New System.Drawing.Size(88, 20) Me.TextBoxSessionCount.TabIndex = 0 Me.TextBoxSessionCount.Text = "0" Me.TextBoxSessionCount.TextAlign = System.Windows.Forms.HorizontalAlignment.Center ' 'ButtonSessionCount ' Me.ButtonSessionCount.Location = New System.Drawing.Point(102, 121) Me.ButtonSessionCount.Name = "ButtonSessionCount" Me.ButtonSessionCount.Size = New System.Drawing.Size(88, 23) Me.ButtonSessionCount.TabIndex = 1 Me.ButtonSessionCount.Text = "Count" ' 'ButtonSessionCountGet ' Me.ButtonSessionCountGet.Location = New System.Drawing.Point(104, 179) Me.ButtonSessionCountGet.Name = "ButtonSessionCountGet" Me.ButtonSessionCountGet.Size = New System.Drawing.Size(88, 23) Me.ButtonSessionCountGet.TabIndex = 2 Me.ButtonSessionCountGet.Text = "Get Count" ' 'SessionCount ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.ButtonSessionCountGet) Me.Controls.Add(Me.ButtonSessionCount) Me.Controls.Add(Me.TextBoxSessionCount) Me.Name = "SessionCount" Me.Text = "SessionCount" Me.ResumeLayout(False) End Sub #End Region Private Sub ButtonSessionCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSessionCount.Click TextBoxSessionCount.Text = wsSessionCount.Count.ToString End Sub End Class
Cliente Web Form:
En este caso, si bien se maneja de la misma forma que un cliente Windows Form, hay que almacenar las cookies en una variable de sesión, para no perder las mismas entre llamados.
Session("Cookies") = CookiesVer el ejemplo siguiente, el manejo recién comentado se encuentra en el método ButtonSessionCount_Click(…):
Public Class SessionCount Inherits System.Web.UI.Page #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents ButtonSessionCount As System.Web.UI.WebControls.Button Protected WithEvents TextBoxSessionCount As System.Web.UI.WebControls.TextBox 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub Private Sub ButtonSessionCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSessionCount.Click Dim Cookies As System.Net.CookieContainer If Session("Cookies") Is Nothing Then Cookies = New System.Net.CookieContainer Else Cookies = Session("Cookies") End If Dim wsSessionCount As New SessionCountWS.SessionCount wsSessionCount.CookieContainer = Cookies TextBoxSessionCount.Text = wsSessionCount.Count.ToString Session("Cookies") = Cookies End Sub End Class
Espacios de nombres usados en el código de este artículo:
System.Web.Services