Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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:

ASP.NET
<%@ 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">--%>
    <form id="form1" runat="server" method="post">
        <div>

            <asp:Button ID="Button_start" runat="server" Text="Start"  />
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <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:

C#
namespace AutoMVCClient.Controllers
{
    public class IndexController : Controller
    {
        
        System.Timers.Timer timer = new System.Timers.Timer();
        WindowServiceHost.WindowServicecodeClient client = new WindowServiceHost.WindowServicecodeClient("NetTcpBinding_IWindowServicecode");
        // GET: /Index/

        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);
        }

      
    }
}
Posted
Updated 7-Dec-16 22:08pm
v2

1 solution

Instead of writing your log start code to index method, create a new method. Call that method when you click any button. This can be done using ajax request or full page post.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900