BasicHttpBinding & SingleContext Services!
I have a WCF serv开发者_高级运维ice that's hosted on IIS 7 using a basicHttpBinding
. The service is acting as a controller for other services distrubted on several machines, known as clients.
Single
ContextMode
with Multiple
ConurrecnyMode
. So that it would keep the state of the object. My problem is after a certain period of inactivity, I see the service invoking the constructor again and thus it loses all its state! Any suggestions to make this service live forever?
Thanks!
It sounds like IIS is recycling your service. Keep this in mind (excerpted from Internet Information Services Hosting Best Practices):
The IIS hosting environment is optimized for services that do not maintain local state in memory. IIS recycles the host process in response to a variety of external and internal events, causing any volatile state stored exclusively in memory to be lost. Services hosted in IIS should store their state external to the process (for example, in a database) or in an in-memory cache that can easily be re-created if an application recycle event occurs.
While it is possible to disable recycling, if you you need to be IIS-hosted and you care about state, you should probably put it in a durable location.
By default IIS recycles application pool every 29 hours.
To avoid this you can go the application pool recycling settings and uncheck this option.
This still will not guarantee 100% state persistence, as the IIS can be restarted, app pool can be recycled when web.config changed, etc. If state is important for you, you might want to persist the state rather to database instead of relying on memory.
Another option to consider is to hosting the service in Windows Service instead of IIS.
精彩评论