I have this strange problem now twice: I am writing this relatevely large web
site on 2.0 and I made a static class, which I use for url encoding and
deconding (for remapping purposes). This static class needs the session
context to encode a url (because I stored the current language there), so I
made a static field of type HttpContext, which I refresh every reqest by
assigning the current context.
Now, every now and then I get this runtime error that the Session property
is not set (Object reference not set to instance of an object) and it's
completely random.
I suspect that it has something to do with the fact that the class is static
and GC, but I can't really figure this out.
I had the same problem in 1.1 a while ago with the same site when I wanted
to make the SqlConnection a static property and connect on Application_Sta rt
and disconnect on Application_End . Every n-th request the SqlConnection
disappeared.
Any suggestions?
I pretty much would like to know why this things happen. 6 1898
The reason is that all requests share the same static variable. Imagine
this scenario:
Request 1 arrives and thread 1 is started to handle it
Thread 1 stores it's context in the static variable
Thread 1 starts working
Request 2 arrives and thread 2 is started to handle it
Thread 2 stores it's context in the static variable
Thread 2 starts working
Thread 1 continues working
Thread 2 uses the static varible - this works fine
Thread 1 uses the static variable - and gets the wrong context
Thread 2 finishes
Thread 2 sends a response to the browser
The context of thread 2 is disposed
Thread 1 tries to use the static variable - and dies
Here you see two problems. The static variable contains the context of
the last request to arrive. That means that some threads will get the
wrong context, and if the last request is first to finish, the static
variable will reference a context that is disposed and no longer has a
reference to a session object.
The solution is to use one instance of the variable for each thread.
This can be done in two ways:
1. Don't use a static class. Create one instance of the class for each page.
2. Use the [ThreadStatic] attribute to create an instance of the static
variable for each thread.
One thing you should be aware of if you choose to use a ThreadStatic
variables, is that the static constructor of the class is only run when
the class is loaded, not for every thread. If you have this code:
[ThreadStatic] static int answer = 42;
The code that initializes the variable is actually placed in the static
constructor. That means that the first thread that uses the class will
get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote: I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context.
Now, every now and then I get this runtime error that the Session property is not set (Object reference not set to instance of an object) and it's completely random.
I suspect that it has something to do with the fact that the class is static and GC, but I can't really figure this out.
I had the same problem in 1.1 a while ago with the same site when I wanted to make the SqlConnection a static property and connect on Application_Sta rt and disconnect on Application_End . Every n-th request the SqlConnection disappeared.
Any suggestions?
I pretty much would like to know why this things happen.
Göran, thank you for the excellent response and explanation! I didn't think
of that a static variable is used in several requests at one time. With this
in mind, it is obviously a suicide to use a static variable, it will most
certainly crash, that explains the randomness of the errors.
On thing that I think is very interesting is that [ThreadStatic] attribute,
I will certainly read everything there is about it.
But is it certain that 1 request is 1 thread and visa versa?
Because if it is not, this will not work as expected. Actually all I need is
request and/or session level storage, but without using the
HttpContext.Cur rent in my Business Logic tier, because that makes it unusable
in every other context.
"Göran Andersson" wrote: The reason is that all requests share the same static variable. Imagine this scenario:
Request 1 arrives and thread 1 is started to handle it Thread 1 stores it's context in the static variable Thread 1 starts working Request 2 arrives and thread 2 is started to handle it Thread 2 stores it's context in the static variable Thread 2 starts working Thread 1 continues working Thread 2 uses the static varible - this works fine Thread 1 uses the static variable - and gets the wrong context Thread 2 finishes Thread 2 sends a response to the browser The context of thread 2 is disposed Thread 1 tries to use the static variable - and dies
Here you see two problems. The static variable contains the context of the last request to arrive. That means that some threads will get the wrong context, and if the last request is first to finish, the static variable will reference a context that is disposed and no longer has a reference to a session object.
The solution is to use one instance of the variable for each thread. This can be done in two ways:
1. Don't use a static class. Create one instance of the class for each page.
2. Use the [ThreadStatic] attribute to create an instance of the static variable for each thread.
One thing you should be aware of if you choose to use a ThreadStatic variables, is that the static constructor of the class is only run when the class is loaded, not for every thread. If you have this code:
[ThreadStatic] static int answer = 42;
The code that initializes the variable is actually placed in the static constructor. That means that the first thread that uses the class will get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote: I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context.
Now, every now and then I get this runtime error that the Session property is not set (Object reference not set to instance of an object) and it's completely random.
I suspect that it has something to do with the fact that the class is static and GC, but I can't really figure this out.
I had the same problem in 1.1 a while ago with the same site when I wanted to make the SqlConnection a static property and connect on Application_Sta rt and disconnect on Application_End . Every n-th request the SqlConnection disappeared.
Any suggestions?
I pretty much would like to know why this things happen.
Vladislav Kosev wrote: Göran, thank you for the excellent response and explanation! I didn't think of that a static variable is used in several requests at one time. With this in mind, it is obviously a suicide to use a static variable, it will most certainly crash, that explains the randomness of the errors.
On thing that I think is very interesting is that [ThreadStatic] attribute, I will certainly read everything there is about it.
But is it certain that 1 request is 1 thread and visa versa?
Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is request and/or session level storage, but without using the HttpContext.Cur rent in my Business Logic tier, because that makes it unusable in every other context.
"Göran Andersson" wrote:
The reason is that all requests share the same static variable. Imagine this scenario:
Request 1 arrives and thread 1 is started to handle it Thread 1 stores it's context in the static variable Thread 1 starts working Request 2 arrives and thread 2 is started to handle it Thread 2 stores it's context in the static variable Thread 2 starts working Thread 1 continues working Thread 2 uses the static varible - this works fine Thread 1 uses the static variable - and gets the wrong context Thread 2 finishes Thread 2 sends a response to the browser The context of thread 2 is disposed Thread 1 tries to use the static variable - and dies
Here you see two problems. The static variable contains the context of the last request to arrive. That means that some threads will get the wrong context, and if the last request is first to finish, the static variable will reference a context that is disposed and no longer has a reference to a session object.
The solution is to use one instance of the variable for each thread. This can be done in two ways:
1. Don't use a static class. Create one instance of the class for each page.
2. Use the [ThreadStatic] attribute to create an instance of the static variable for each thread.
One thing you should be aware of if you choose to use a ThreadStatic variables, is that the static constructor of the class is only run when the class is loaded, not for every thread. If you have this code:
[ThreadStatic] static int answer = 42;
The code that initializes the variable is actually placed in the static constructor. That means that the first thread that uses the class will get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote: I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context.
Now, every now and then I get this runtime error that the Session property is not set (Object reference not set to instance of an object) and it's completely random.
I suspect that it has something to do with the fact that the class is static and GC, but I can't really figure this out.
I had the same problem in 1.1 a while ago with the same site when I wanted to make the SqlConnection a static property and connect on Application_Sta rt and disconnect on Application_End . Every n-th request the SqlConnection disappeared.
Any suggestions?
I pretty much would like to know why this things happen.
Is there any chance that a request can be handled by more than one thread? Is
this ThreadStatic reliable?
"Göran Andersson" wrote: Vladislav Kosev wrote: Göran, thank you for the excellent response and explanation! I didn't think of that a static variable is used in several requests at one time. With this in mind, it is obviously a suicide to use a static variable, it will most certainly crash, that explains the randomness of the errors.
On thing that I think is very interesting is that [ThreadStatic] attribute, I will certainly read everything there is about it.
But is it certain that 1 request is 1 thread and visa versa?
Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is request and/or session level storage, but without using the HttpContext.Cur rent in my Business Logic tier, because that makes it unusable in every other context.
"Göran Andersson" wrote:
The reason is that all requests share the same static variable. Imagine this scenario:
Request 1 arrives and thread 1 is started to handle it Thread 1 stores it's context in the static variable Thread 1 starts working Request 2 arrives and thread 2 is started to handle it Thread 2 stores it's context in the static variable Thread 2 starts working Thread 1 continues working Thread 2 uses the static varible - this works fine Thread 1 uses the static variable - and gets the wrong context Thread 2 finishes Thread 2 sends a response to the browser The context of thread 2 is disposed Thread 1 tries to use the static variable - and dies
Here you see two problems. The static variable contains the context of the last request to arrive. That means that some threads will get the wrong context, and if the last request is first to finish, the static variable will reference a context that is disposed and no longer has a reference to a session object.
The solution is to use one instance of the variable for each thread. This can be done in two ways:
1. Don't use a static class. Create one instance of the class for each page.
2. Use the [ThreadStatic] attribute to create an instance of the static variable for each thread.
One thing you should be aware of if you choose to use a ThreadStatic variables, is that the static constructor of the class is only run when the class is loaded, not for every thread. If you have this code:
[ThreadStatic] static int answer = 42;
The code that initializes the variable is actually placed in the static constructor. That means that the first thread that uses the class will get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote: I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context.
Now, every now and then I get this runtime error that the Session property is not set (Object reference not set to instance of an object) and it's completely random.
I suspect that it has something to do with the fact that the class is static and GC, but I can't really figure this out.
I had the same problem in 1.1 a while ago with the same site when I wanted to make the SqlConnection a static property and connect on Application_Sta rt and disconnect on Application_End . Every n-th request the SqlConnection disappeared.
Any suggestions?
I pretty much would like to know why this things happen.
IIS uses one thread to handle a request. If it started more than one
thread to handle a request, that would mean that there would be more
than one response, and that of course wouldn't work.
You can start new threads from your code, but then you have to take
responsibility for those.
Vladislav Kosev wrote: Is there any chance that a request can be handled by more than one thread? Is this ThreadStatic reliable?
"Göran Andersson" wrote:
Vladislav Kosev wrote: Göran, thank you for the excellent response and explanation! I didn't think of that a static variable is used in several requests at one time. With this in mind, it is obviously a suicide to use a static variable, it will most certainly crash, that explains the randomness of the errors.
On thing that I think is very interesting is that [ThreadStatic] attribute, I will certainly read everything there is about it.
But is it certain that 1 request is 1 thread and visa versa? Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is request and/or session level storage, but without using the HttpContext.Cur rent in my Business Logic tier, because that makes it unusable in every other context.
"Göran Andersson" wrote:
The reason is that all requests share the same static variable. Imagine this scenario:
Request 1 arrives and thread 1 is started to handle it Thread 1 stores it's context in the static variable Thread 1 starts working Request 2 arrives and thread 2 is started to handle it Thread 2 stores it's context in the static variable Thread 2 starts working Thread 1 continues working Thread 2 uses the static varible - this works fine Thread 1 uses the static variable - and gets the wrong context Thread 2 finishes Thread 2 sends a response to the browser The context of thread 2 is disposed Thread 1 tries to use the static variable - and dies
Here you see two problems. The static variable contains the context of the last request to arrive. That means that some threads will get the wrong context, and if the last request is first to finish, the static variable will reference a context that is disposed and no longer has a reference to a session object.
The solution is to use one instance of the variable for each thread. This can be done in two ways:
1. Don't use a static class. Create one instance of the class for each page.
2. Use the [ThreadStatic] attribute to create an instance of the static variable for each thread.
One thing you should be aware of if you choose to use a ThreadStatic variables, is that the static constructor of the class is only run when the class is loaded, not for every thread. If you have this code:
[ThreadStatic] static int answer = 42;
The code that initializes the variable is actually placed in the static constructor. That means that the first thread that uses the class will get the value 42, but for every thread after that, the value will be zero.
Vladislav Kosev wrote: > I have this strange problem now twice: I am writing this relatevely large web > site on 2.0 and I made a static class, which I use for url encoding and > deconding (for remapping purposes). This static class needs the session > context to encode a url (because I stored the current language there), so I > made a static field of type HttpContext, which I refresh every reqest by > assigning the current context. > > Now, every now and then I get this runtime error that the Session property > is not set (Object reference not set to instance of an object) and it's > completely random. > > I suspect that it has something to do with the fact that the class is static > and GC, but I can't really figure this out. > > I had the same problem in 1.1 a while ago with the same site when I wanted > to make the SqlConnection a static property and connect on Application_Sta rt > and disconnect on Application_End . Every n-th request the SqlConnection > disappeared. > > Any suggestions? > > I pretty much would like to know why this things happen. >
Thanks a lot!
"Göran Andersson" wrote: IIS uses one thread to handle a request. If it started more than one thread to handle a request, that would mean that there would be more than one response, and that of course wouldn't work.
You can start new threads from your code, but then you have to take responsibility for those.
Vladislav Kosev wrote: Is there any chance that a request can be handled by more than one thread? Is this ThreadStatic reliable?
"Göran Andersson" wrote:
Vladislav Kosev wrote: Göran, thank you for the excellent response and explanation! I didn't think of that a static variable is used in several requests at one time. With this in mind, it is obviously a suicide to use a static variable, it will most certainly crash, that explains the randomness of the errors.
On thing that I think is very interesting is that [ThreadStatic] attribute, I will certainly read everything there is about it.
But is it certain that 1 request is 1 thread and visa versa? Yes. Every request is handled in a separate thread.
Because if it is not, this will not work as expected. Actually all I need is request and/or session level storage, but without using the HttpContext.Cur rent in my Business Logic tier, because that makes it unusable in every other context.
"Göran Andersson" wrote:
> The reason is that all requests share the same static variable. Imagine > this scenario: > > Request 1 arrives and thread 1 is started to handle it > Thread 1 stores it's context in the static variable > Thread 1 starts working > Request 2 arrives and thread 2 is started to handle it > Thread 2 stores it's context in the static variable > Thread 2 starts working > Thread 1 continues working > Thread 2 uses the static varible - this works fine > Thread 1 uses the static variable - and gets the wrong context > Thread 2 finishes > Thread 2 sends a response to the browser > The context of thread 2 is disposed > Thread 1 tries to use the static variable - and dies > > Here you see two problems. The static variable contains the context of > the last request to arrive. That means that some threads will get the > wrong context, and if the last request is first to finish, the static > variable will reference a context that is disposed and no longer has a > reference to a session object. > > The solution is to use one instance of the variable for each thread. > This can be done in two ways: > > 1. Don't use a static class. Create one instance of the class for each page. > > 2. Use the [ThreadStatic] attribute to create an instance of the static > variable for each thread. > > One thing you should be aware of if you choose to use a ThreadStatic > variables, is that the static constructor of the class is only run when > the class is loaded, not for every thread. If you have this code: > > [ThreadStatic] static int answer = 42; > > The code that initializes the variable is actually placed in the static > constructor. That means that the first thread that uses the class will > get the value 42, but for every thread after that, the value will be zero. > > > Vladislav Kosev wrote: >> I have this strange problem now twice: I am writing this relatevely large web >> site on 2.0 and I made a static class, which I use for url encoding and >> deconding (for remapping purposes). This static class needs the session >> context to encode a url (because I stored the current language there), so I >> made a static field of type HttpContext, which I refresh every reqest by >> assigning the current context. >> >> Now, every now and then I get this runtime error that the Session property >> is not set (Object reference not set to instance of an object) and it's >> completely random. >> >> I suspect that it has something to do with the fact that the class is static >> and GC, but I can't really figure this out. >> >> I had the same problem in 1.1 a while ago with the same site when I wanted >> to make the SqlConnection a static property and connect on Application_Sta rt >> and disconnect on Application_End . Every n-th request the SqlConnection >> disappeared. >> >> Any suggestions? >> >> I pretty much would like to know why this things happen. >> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lester |
last post by:
I have a low level question . . .
If I have an assembly with a single class with a static int property, I know
the assembly will get loaded the first time the class is referenced. At that
point, I now have a "never been explicitly instantiated object" in memory
that can be accessed by all code that was built with a reference to the...
|
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I am
seeking to determine whether I am programming non-standard C++ or if the
problem lies elsewhere.
To summarize static const class members are not being accessed properly
when accessed from a...
|
by: Samee Zahur |
last post by:
Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local...
|
by: Adam Smith |
last post by:
Hi. I have a static arraylist in a class which is populated during
the class's static constructor.
Will this arraylist or the class objects allocated on it ever be
garbage collected if I don't access it for a few hours? Other posts
to this board seem to point to no.
Thanks in advance.
Adam Smith
|
by: Atul Malaviya |
last post by:
>From a design/usability perspective.
When will one use a singleton pattern and when a class with purely
static members?
What are the pros and cons? I have inherited a code base which is full
of both these and I am a bit confused on this count.
Singleton insures one object of a class in the application. A class
with purely static...
| |
by: J |
last post by:
I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages.
Here is a sample
public class statictes
private statictest() {} // static onl
private static string data = null
|
by: Kevin Prichard |
last post by:
Hi all,
I've recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I'd like to discuss with you experts.
I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of...
|
by: Chuck Cobb |
last post by:
I am creating some static classes with static methods and static variables,
but I have a question: What is the lifetime of static classes and static
variables? Is there any risk that these items might be garbage collected
if they're not used for a period of time?
For example:
public static class test
{
static Collection<string> coll;
|
by: archana |
last post by:
Hi all,
can anyone tell me differene between public static and private static
method.
how they are allocated and access?.
thanks in advance.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |