views:

1181

answers:

3

My webservice constructor is getting called each time that I call a webmethod. This is causing some problems with some new functionality that I am adding and I cannot figure out what I am doing wrong to cause this. The only place that I am newing the webservice is in global.asax.cs's Application_Start, but if I remove the code to new the webservice, it still calls the constructor each time that I call a webmethod.

I have tested this by browsing to the .asmx file and calling the webmethod's that way and I have also created a test console application that has a reference to the webservice and will call the methods that way. Both of these give the same results.

I am using c# in ASP.NET 1.1. Any ideas?

Edit:
I am trying to create a heartbeat thread that polls a windows service. I am attempting to save the result in a hash table (multiple threads polling multiple services). I have changed the webmethod (as it used to poll the windows service) to get the results from the hashtable. We are trying to increase the speed of this webmethod call.

+7  A: 

Whenever you call a web method, a new instance of the web service is created - this is the intended behaviour. Web services are stateless, which means that an instance of the service is not kept alive between web method calls, and therefore you cannot store any state in members (fields, properties) of the web service.

If you want to save some values/state between web method calls, you have to use the ASP.NET session or a database for that purpose.

See this question for details: http://stackoverflow.com/questions/808168/is-this-supposed-to-work-this-way

M4N
A: 

Err... this is kind of by design I'm afraid. Can you describe the problems you are getting and what you are trying to acheive, I think you need to refactor your design a little here.

Steve Haigh
Edited the original post to describe what we are trying to do.
Jeremy Cron
A: 

You can also switch to WCF which supports stateful services

Pablote
unfortunately we are 'stuck' with the 1.1 framework for this version
Jeremy Cron