Assigning session object within thread

  • Thread starter Thread starter William Mild
  • Start date Start date
W

William Mild

I'm trying to update a session object in a web
application from within a thread. I need help fixing
this error.

ERROR: An unhandled exception of
type 'System.Runtime.Serialization.SerializationException'
occurred in Unknown Module.

Additional information: The type System.Web.HttpException
in Assembly System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not
marked as serializable.

CONFIGURATION: XP Professional SP1 running VS.NET 2003.
This is a Web application.

STEPS TO REPRODUCE:

Add web form to project and move to code behind. Paste
the following code:

Imports System.Threading

Public Class test
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

Dim thisReportCard As New ReportCard2

Dim NewThread As Thread = New Thread(AddressOf
thisReportCard.IndividualSS)
NewThread.Start()

End Sub

End Class


Public Class ReportCard2
Inherits System.Web.UI.Page

Dim ThreadVar As String = "lock me"


Public Sub IndividualSS()

SyncLock (Me)
Session("PercentOfProgress") = 60
End SyncLock

End Sub

End Class
 
Hi William,
Same situation.
When I tested it I have extended your test (maybe you did too).
And it goes only wrong in this situation is when the "session" is in a
thread.
A "variable" can be in a thread no problem.
A "session" can be in another class "no thread" no problem.
I did made the test beneath to be sure if it was, and according this test I
thought so.
What to do about it, maybe there is someone else who can tell it to us both.
I hope this give you the feeling it is not your system.
Cor

\\\
Public Class Test1
Inherits System.Web.UI.Page
Public Shared aa As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim thisReportCard2 As New ReportCard2
Dim thisReportCard3 As New ReportCard3
Dim NewThread As Thread = New Thread(AddressOf
thisReportCard2.IndividualS2)
NewThread.Start()
thisReportCard3.IndividualS3(aa)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.TextBox1.Text = Session("aa").ToString
End Sub
End Class
Public Class ReportCard2
Inherits System.Web.UI.Page
Public Sub IndividualS2()
Test1.aa = "60"
End Sub
End Class
Public Class ReportCard3
Inherits System.Web.UI.Page
Public Sub IndividualS3(ByVal aa As String)
Session("aa") = aa
End Sub
End Class
////
 
Hello,

I run into same error with the code. I need to do additional research to
determine what the problem is. I will post my result as soon as possible.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi William,

I used following code instead and found the actural error:

Public Sub IndividualSS()
Try

Session("PercentOfProgress") = 60
Catch mye As System.Web.HttpException
logerror(mye.Message)
End Try


End Sub

The error message is:

"Session state can only be used when enableSessionState is set to true,
either in a configuration file or in the Page directive"

Even your class "ReportCard2" inherits from System.Web.UI.Page, it didn't
set EnableSessionState to true, and this cause the error.

To get around the problem, we may add a new web form and add 'Public Sub
IndividualSS()" to it. Add:

Dim thisReportCard As New WebForm2

Dim NewThread As Thread = New Thread(AddressOf
thisReportCard.IndividualSS)

NewThread.Start()

This worked well on my side.

Hope this help,

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Luke,

I am not sure of I understand you totaly, but I thought that my first
thoughts were the same as yours.
I made a test, did you try that too.
That works while the session state is in a same type class as in the first
example not in a thread.
And a value is changed in a class in the same way that is in a thread.
That sample works fine.

Problem is not to overcome it I think, for that are a lot of possibilites
(one is just transfering the session values from the main thread)
Question is, why is this strange behaviour.
It could be my test is wrong, but it is in my message try it?.
(And please don't look how I did give the variable from the the thread to
the main, I did not want to take more time and it did work :-)

Cor
 
Hi William,

In your previous code, ReportCard2 is a class inheriting from
System.Web.UI.Page. However, it is not a Editable WEB Form in the project,
it is only a class. My suggestion is to add a new web form (editable) and
use

Dim thisReportCard As New WebForm2

instead of


Dim thisReportCard As New ReportCard2


Hope this answer your question,


Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top