I'm new to WCF, so maybe this is something best done in another way.
Right now I have a collection of WCF Services, but I am trying to build in functionality which sends weekly emails. To do this I built another WCF service with the code below:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, AutomaticSessionShutdown = false)]
public class TimerService : ITimerService
{
private static Timer timer;
private static TimeSpan tSpan = new TimeSpan(0, 0, 20, 0);
private static OtherService Ref = new OtherService();
public void ToggleEmailTimer(bool enabled)
{
if (enabled)
timer = new Timer(new TimerCallback(TimerElapsed), null, tSpan, tSpan);
else
{
if(timer != null)
timer.Dispose();
}
}
private void TimerElapsed(object state)
{
Ref.SendWeekly();
}
}
It begins disabled, and I enable it from an aspx page.For testing, I've managed to make this work for 10 minutes intervals, and it seems to break somewhere around setting the interval to 15 minutes.
To me, it seems like the WCF Serivce session is expiring from inactivity, which would explain why the timer just stops. Is there a way to specify the lifetime of the WCF Service so that I could enabled the timer from the aspx page, exit, and the timer service will persist? I have seen information about setting timeout values but it is still unclear to me if this applies.