Re: Going crazy trying to understand Web Services behaviour with static member fields, static classes and singleton classes.
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 12 Jun 2007 13:50:55 -0400
That is not the case. In a Web Service hosted in ASP.NET, the assembly
is loaded for the life of the app domain (ASP.NET can recycle the app domain
mind you) and therefore the static values will exist for the life of the app
domain.
It doesn't seem like you would want this kind of functionality, because
the lifecycle of a user's interaction with your app goes beyond one single
page request. I imagine that a user will use multiple pages throughout
their session.
If you want to equate user sessions with the experience in a single run
of a Windows Forms app, then you can't use static members. The class would
have to contain the value in the instance, and then you would have to have
specific instances for each session on the webserver.
For windows forms apps, using static works because there is only one
session. Unfortunately, that is not the case with a web service/web app in
general.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"BLUE" <blue> wrote in message news:exD7$hRrHHA.4804@xxxxxxxxxxxxxxxxxxxxxxx
FirstClass members:
- static int counter;
- SingletonClass sc = SingletonClass.Instance;
Moreovere FirstClass uses a static class named SecondClass with a static
property SecondClass.Description.
In a normal windows application:
- if in the Main method I create 2 or more instances of FirstClass, they
all see the same counter, the same SingletonClass.Instance and the same
SecondClass.Description
- if I launch the exe 2 or more times each exe sees different things
What does it happen with web services???
I think a web service is like a web page, so it lives only for the
duration of the execution of a webmethod like an exe lives only until we
click on the x: each call should reset counter to 0 so but this is not
true!
Why???
Logging on a text file I have discovered that two sequential calls to the
same web method refers to the same SingletonClass.Instance so I've thought
that I can declare a static DateTime and execute a method only if last
call was 24 hours ago.
To my surprise that DateTime is reset on each subsequent call but not if
two calls are simultaneous.
I do not understand what is the exact behaviour of Web Services:
subsequent calls are like 2 launch of the same exe and simultaneous ones
like 2 creation of FirstClass from the Main method?
Thanks,
Luigi.
.
- References:
- Prev by Date: Re: How to extract full type name from assembly qualified name
- Next by Date: Re: To those who like pleonasms in writing
- Previous by thread: Going crazy trying to understand Web Services behaviour with static member fields, static classes and singleton classes.
- Next by thread: Re: Going crazy trying to understand Web Services behaviour with static member fields, static classes and singleton classes.
- Index(es):
Relevant Pages
|