[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Kris van der Mast

1/13/2003 2:17:00 PM

Hi,

I want to create a hitcounter based on a session (per user). The code from
this is:



Imports System.Web.Services



<WebService(Namespace := "http://tempuri....)> _

Public Class Service1

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(Description:="Hitcounter Test...", EnableSession:=True)>
Public Function HitCounter() As Integer

If Session("hit") Is Nothing Then

Session("hit") = 1

Else

Session("hit") = CType(Session("hit"), Integer) + 1

End If

Return CType(Session("hit"), Integer)

End Function



End Class




When I create a webform in this same project as the webservice then I can
get the hitcounter correctly. But of course this isn't the way to use
webservices so I created another project on which I referenced the
webservice and implemented the following code:



Imports hitcounterTest.localhost



Public Class WebForm1

Inherits System.Web.UI.Page

Protected WithEvents Displaylbl As System.Web.UI.WebControls.Label



#Region " Web Form Designer Generated Code "



'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()



End Sub



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

Dim ws As New Service1()

Displaylbl.Text = ws.HitCounter.ToString

End Sub



End Class





But when I build and test it, I seem to lose the session on the webservice
side. It seems to me that every time a new session is created. Does anyone
know how I can fix this problem?

Thanks in advance, Kris.


1 Answer

Pierre Greborio

1/13/2003 5:17:00 PM

0

The problem is that you don't have a cookie container on the client. Then,
you should set a CookieContainer to your proxy class:

' this must be global
Dim container As New CookieContainer()

Dim ws As New Service1()
ws.CookieContainer = container
Displaylbl.Text = ws.HitCounter.ToString

Be aware that the container must be globa since it must be the same for each
call since it is part of the single session.

Pierre
--
-----------------------------------------------------------
Pierre Greborio
http://www.ugi...
-----------------------------------------------------------
"Kris van der Mast" <kris.vandermast@telepolis.antwerpen.be> wrote in
message news:OU25aZwuCHA.2556@TK2MSFTNGP10...
> Hi,
>
> I want to create a hitcounter based on a session (per user). The code from
> this is:
>
>
>
> Imports System.Web.Services
>
>
>
> <WebService(Namespace := "http://tempuri....)> _
>
> Public Class Service1
>
> 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(Description:="Hitcounter Test...", EnableSession:=True)>
> Public Function HitCounter() As Integer
>
> If Session("hit") Is Nothing Then
>
> Session("hit") = 1
>
> Else
>
> Session("hit") = CType(Session("hit"), Integer) + 1
>
> End If
>
> Return CType(Session("hit"), Integer)
>
> End Function
>
>
>
> End Class
>
>
>
>
> When I create a webform in this same project as the webservice then I can
> get the hitcounter correctly. But of course this isn't the way to use
> webservices so I created another project on which I referenced the
> webservice and implemented the following code:
>
>
>
> Imports hitcounterTest.localhost
>
>
>
> Public Class WebForm1
>
> Inherits System.Web.UI.Page
>
> Protected WithEvents Displaylbl As System.Web.UI.WebControls.Label
>
>
>
> #Region " Web Form Designer Generated Code "
>
>
>
> 'This call is required by the Web Form Designer.
>
> <System.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
>
>
>
> End Sub
>
>
>
> 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
>
> Dim ws As New Service1()
>
> Displaylbl.Text = ws.HitCounter.ToString
>
> End Sub
>
>
>
> End Class
>
>
>
>
>
> But when I build and test it, I seem to lose the session on the webservice
> side. It seems to me that every time a new session is created. Does anyone
> know how I can fix this problem?
>
> Thanks in advance, Kris.
>
>