I want to create a LogFile using TimerControl in MVC application.
I have created WCF service and hosted in windows server. Now I need to consume my service in MVC 4 Application, By using "Start" and "Stop" Button. When i click Start Button, Service should start, if i click stop, Service should stop.
I have achieved in Windows application by consuming WCF. But iam New to MVC, I dont no how to write inside start and stop.
But i have written in Index, When webapplication is loaded, My log is get started, But Log should start when i click "start" button. Could you please help me.
What I have tried:
Here is the code for WCF Service:
namespace WindowServicecode
{
public class WindowServicecode : IWindowServicecode
{
public void Getmessage(string message)
{
FileStream fs = new FileStream(@"d:\ScheduledService.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(message);
sw.Flush();
sw.Close();
}
}
}
View:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<script runat="server">
</script>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<%----%>
<form id="form1" runat="server" method="post">
<div>
<asp:Button ID="Button_start" runat="server" Text="Start" />
<asp:Button ID="Button_stop" runat="server" Text="Stop" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Select Start Button to Start the service"></asp:Label>
</div>
</form>
</body>
</html>
Index:
namespace AutoMVCClient.Controllers
{
public class IndexController : Controller
{
System.Timers.Timer timer = new System.Timers.Timer();
WindowServiceHost.WindowServicecodeClient client = new WindowServiceHost.WindowServicecodeClient("NetTcpBinding_IWindowServicecode");
public ActionResult Index()
{
client.Getmessage("start service" + DateTime.Now);
timer.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsedTime);
timer.Interval = 10000;
timer.Enabled = true;
return View();
}
private void OnElapsedTime(object source, System.Timers.ElapsedEventArgs e)
{
client.Getmessage("Another entry at " + DateTime.Now);
}
}
}