views:

704

answers:

1

When a client makes a web service request, how does asp.net assign an instance of the service class to handle that request?

Is a new instance of the service class created per request or is there pooling happening or is there a singleton instance used to handle all requests?

+1  A: 

For classic ASMX services you definitely get a new instance with each request, just like an ASPX request. For a WCF service (.SVC) you do have more options, such as running as a singleton.

If you are interested in doing work with a singleton and pooling you can use the ASMX service simply as the lightweight proxy to pass the parameters back and forth. Your implementation of the service could be a singleton that lives with the App Pool for your web site. You will need to account for the App Pool being reset occasionally as that is how IIS manages ASP.NET sites.

What you can also do is run a Windows Service with a WCF service that is always running. This service would listen to localhost on an endpoint only accessible from the same machine. You can then have the ASMX service call to the WCF service locally. This will allow you to always ensure your state is alive as long as you like even when IIS restarts the App Pool. Naturally, you can also change the security for the WCF Windows Service to allow access remotely with a password if you want to allow multiple web services to use the same service host for the purpose of improved resource usage.

Brennan