Timer inside a WCF Singleton hosted in a Windows Service (over webHttpBinding) dies unexpectedly?

The most likely cause for your issue is InstanceContextMode If you want your service instance to always be in memory you should use Single. You probably have PerSession or PerCall and that would explain why your timer is disappearing. You mention that your service is singleton but the symptoms are very suspicious.

The service instance stays in memory until you shutdown host ServiceBehavior( ConcurrencyMode = ConcurrencyMode. Multiple, InstanceContextMode = InstanceContextMode. Single ) From WCF instance management : The singleton service lives forever, and is only disposed of once the host shuts down.

The singleton is created exactly once when the host is created EDIT: You probably checked that windows service is still running when your listener stops listening and timer disappears. It would also make sense to see if ServiceHost itself stays in memory. You can also put some logging in ServiceHosts 'Closing', 'Closed' and 'Faulted' event handlers EDIT 2: If your timer is disappearing than you should look at how you allocate it.It most likely gets garbage collected.

You have to declare it as an instance field that is reachable from live objects. Make it static to be absolutely sure. You do it for DataTimer but it is not clear how the timer is declared and allocated inside DataTimer Post some code please EDIT 3: You should not create timers in the operation.

What happens if operation get called more than once? What happens to the old timer? I don't see how you close/dispose it.

You also seem to have two constructors for DataTimer. One of them is doing nothing. And on top of that you have separate list of timers.

This is a bit convoluted. Please isolate the problem and maybe post new code after that.

The most likely cause for your issue is InstanceContextMode. If you want your service instance to always be in memory you should use Single. You probably have PerSession or PerCall and that would explain why your timer is disappearing.

You mention that your service is singleton but the symptoms are very suspicious. The service instance stays in memory until you shutdown host. ServiceBehavior( ConcurrencyMode = ConcurrencyMode.

Multiple, InstanceContextMode = InstanceContextMode. Single ) From WCF instance management: The singleton service lives forever, and is only disposed of once the host shuts down. The singleton is created exactly once when the host is created.

EDIT: You probably checked that windows service is still running when your listener stops listening and timer disappears. It would also make sense to see if ServiceHost itself stays in memory. You can also put some logging in ServiceHosts 'Closing', 'Closed' and 'Faulted' event handlers.

EDIT 2: If your timer is disappearing than you should look at how you allocate it. It most likely gets garbage collected. You have to declare it as an instance field that is reachable from live objects.

Make it static to be absolutely sure. You do it for DataTimer but it is not clear how the timer is declared and allocated inside DataTimer. Post some code please.

EDIT 3: You should not create timers in the operation. What happens if operation get called more than once? What happens to the old timer?

I don't see how you close/dispose it. You also seem to have two constructors for DataTimer. One of them is doing nothing.

And on top of that you have separate list of timers. This is a bit convoluted. Please isolate the problem and maybe post new code after that.

It is actually Single. – user96403 Aug 15 at 7:47 Please see updated answer. – Dmitry Aug 15 at 13:19 ok, I will add logging to these evenhandlers and/or also create a little timed component that pings my timer service and keeps it alive – user96403 Aug 17 at 13:11 1 May be a good idea to actually figure out what's causing this issue before adding 'keep alive' component.

You would be making your service less reliable and dependent on this pinging component. It may also disappear :) – Dmitry 03 Aug3 at 13:24 I see your point there. Actually after further analysis, I found the singleton stays in memory as documented.It is just the System.Timers.

Timer instance variable that dies!. I can see that all other instance variables are still there. Looks like Timer's an issue here.

– user96403 Aug 19 at 9:51.

I've not come across this issue specifically - however, if you just want the timer running while the service is running why not make it static. Then your instance context mode and instance lifetime won't affect your functionality.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions