Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a .NET Web Service instantiate with each method call?

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?

like image 557
Richard210363 Avatar asked Jul 17 '09 16:07

Richard210363


2 Answers

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.

like image 196
Justin Niessner Avatar answered Oct 21 '22 05:10

Justin Niessner


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.

like image 21
Steven Sudit Avatar answered Oct 21 '22 07:10

Steven Sudit