Harsha Shetty

Harsha Shetty

  • NA
  • 9
  • 875

Timer functinality is IIS deployed webapp

Aug 27 2019 2:17 AM
Hi Team,
 
I have deployed an MVC webapp into IIS.Functionalitywise its working fine.But I am wtiring to text files at every 10seconds using System.Timers.Timer.But timer is not getting triggered and hence no information is written to text files.How to overcome this? Timer functionality works fine if I am running the app from Visual Studio.
 
Code piece:
 
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 30000; // 30 seconds, change this as required
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
timer.Enabled = true;
timer.Start();
void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
timer.Stop();
string filepaths = Dirpath + "Infologsssss.txt";
Log.ToFile(0, filepaths);
// Directory.CreateDirectory(path);
StreamServerApp.keepAlive();
FileStream fst = new FileStream(path + "Timer.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter swt = new StreamWriter(fst);
swt.WriteLine("Timer last called at " +DateTime.Now);
swt.Flush();
swt.Close();
fst.Close();
string url = "http://localhost:8080/";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
// req.KeepAlive = true;
req.Method = "GET";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode != HttpStatusCode.OK)
{
StreamWriter swf8080 = new StreamWriter(path + "Testing8080fail.txt", append: true);
swf8080.WriteLine("Response status NOT OK 8080 " + DateTime.Now);
swf8080.Close();
StreamServerApp.keepAlive();
if (_Server.IsRunning != true)
{
_Server.Start(8080);
}
}
else
{
StreamWriter sws8080 = new StreamWriter(path + "Testing8080success.txt", append: true);
sws8080.WriteLine("Response status OK 8080 " + DateTime.Now);
sws8080.Close();
}
resp.Close();
timer.Start();

Answers (1)