Going crazy trying to understand Web Services behaviour with static member fields, static classes an

B

BLUE

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


BLUE said:
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.
 

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

Top