473,385 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1618
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
2
by: Jonas Cord | last post by:
Hi, I'm trying to learn proxy classes in C++ so I've written the following code, trying to modify an example given in Deitel & Deitel's C++ book. I am trying to "hide" details of the original...
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
9
by: Joanna Carter \(TeamB\) | last post by:
Following on from the other discussion, I have to just check something out with reference to disposal of resources held in static fields. I have a Persistence Framework that is 'globally...
7
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...
3
by: mark.norgate | last post by:
Hello I'm writing an application in ASP.NET 1.1 and have come across a problem using static fields in my page classes. I have lots of controls on the page that all need to bind to the same...
2
by: utab | last post by:
Dear all, I am confused at some point on the initialization of static map member of a class. The class I have designed id something like this. class Class_name{ public: private:
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
10
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine*...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.