R – Does a .NET Web Service instantiate with each method call

netweb services

I'm trying out web services for an idea I've had.
Running in debug it looks like the web service class instantiates each time a client calls a method in the web service. I can see this by seeing that the constructor gets called each time I call a method.
I only instantiate the proxy web service once in the client.

This would mean I would have to store all data between calls and means that if I use a databse I'll have to re-connect with for every call to a method.

That can't be correct?

Best Answer

For ASMX, it's correct and largely a good thing. Services should be as stateless as possible, and if you really need to store something between calls then you can use a singleton. I don't think holding onto a database connection would qualify, though, because they're cached and you want to scope your use, anyhow.

If you want a single object to stay alive between calls, WCF does offer this option. Given that ASMX is obsolete, you might want to move to WCF.