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?
That is indeed correct.
Web Services (in a Service Oriented Architecture) are meant to be stateless (they don't remember anything between calls...all data persistance is up to you).
If you search long enough on the web, you'll find attempts to create stateful web services (which do have their place, but go against the core principals of SOA). You'll find, though, that they don't alleviate your concerns. You'll get a new class instantianciation with every call and any connections to the DB will have to be re-created.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With