How can I keep a WCF service alive between requests?
The service is creating a simple cache in memory on initialisation and I dont want the service to have to re-initialise between requests.
The cache is built up using data extracted from a database and so for efficiency i only need this to be done rarely.
The database is not going to be updated often and so recycling the app pool every night will suffice in terms of updating the cache.
views:
37answers:
1
+2
A:
You are asking two questions. If you need service instance alive between requests you should check InstanceContextMode set to PerSession or Single. If you need application initialization to prepare your shared cache you should check IIS 7.5 Warm-up module and place your initialization to Application_Start in Global.asax. For older versions of IIS you have to write some application (console is enough) which will be scheduled to call your web application / service and warm it up.
Ladislav Mrnka
2010-10-05 16:22:24
The InstanceContextMode set to single didnt seem to work between connections. I'm setting up the in memory data within the services static constructor. If I make subsequent calls from sperate clients then this constructor is called each time. I'm not too knowledgable on IIS so I may be missing something fundamental here.
gouldos
2010-10-05 16:37:31
Then something is wrong with your application because static constructor should be called only once (as well as non static constructor for service defined as Single). Why does your application restart?
Ladislav Mrnka
2010-10-05 17:01:14
Unfortunately i cant make use of the warm up module due to internal constraints, however the delay in initialising isnt too great that it would be an issue.
gouldos
2010-10-06 08:58:55
The question of why the wcf service is restarting on every request is bafflin me. Are there any specific settings that would make this happen? Currently its running in its own app pool which has a recycling time specified at 2am and an idle timeout of >24hrs. I havent changed anything else. If I run my client call (on seperate instances of a client proxy) twice in succession then its definately calling the static constructor twice. The worker process for the app pool is still running so I can only guess that a new appDomain is being created within the process between connections.
gouldos
2010-10-06 09:04:39
After a bit of testing I've found out that it was the code in my static constructor that was causing the app domain to be recycled and so loose the reference to the service type. I was appending a file in the services bin folder every time the static constructor was called. Apparently altering a file or the structure under tha application root will cause the app domain to be recycled. Doh!
gouldos
2010-10-06 10:01:05
Yes, modification of bin content is one of several things which cause application to restart.
Ladislav Mrnka
2010-10-06 10:46:10