468,310 Members | 1,418 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,310 developers. It's quick & easy.

Going crazy trying to understand Web Services behaviour with static member fields, static classes and singleton classes.

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.
Jun 12 '07 #1
2 1471
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]
- mv*@spam.guard.caspershouse.com

"BLUE" <bluewrote in message news:ex**************@TK2MSFTNGP04.phx.gbl...
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.

Jun 12 '07 #2
Hi,
"BLUE" <bluewrote in message news:ex**************@TK2MSFTNGP04.phx.gbl...
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???

Cause in a webservice escenario is like if you are using the SAME
executable.

Imagine that in your win apps bith apps use a third app (think of it as a
hidden app) that is where is hosted the classes you are using

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???
Wrong, if you see the docs you will see that the webservice is created by
the IIS as a SINGLETON, so all the calling clients will share the same copy.
Jun 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jonas Cord | last post: by
33 posts views Thread by Chris Capel | last post: by
9 posts views Thread by Joanna Carter \(TeamB\) | last post: by
3 posts views Thread by mark.norgate | last post: by
2 posts views Thread by utab | last post: by
10 posts views Thread by JoeC | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.