locked
Service with web-based front end? RRS feed

  • Question

  • User1373395183 posted

    I have lots of coding experience but not a lot of web app experience, bear with me.

    I want to write a program that is running most of the time in an endless loop. Every 1 minute it writes the date and time to a text file on the hard drive. The program needs to have a web based front end which is hosted on the same machine, and allows me to stop or resume the loop from a web browser.

    The only way I can think of to solve this in .NET is to write the loop portion as a service, so it will run in the background. Then I'd write an ASP GUI with a single button to start or stop that service.


    I have a feeling there's a better way.  If someone can give me a rough outline of the structure I'm sure I can figure it out.


    Thanks!

    Friday, March 26, 2010 10:52 PM

Answers

  • User-525215917 posted

    I want to write a program that is running most of the time in an endless loop. Every 1 minute it writes the date and time to a text file on the hard drive. The program needs to have a web based front end which is hosted on the same machine, and allows me to stop or resume the loop from a web browser.

    When you have to do something in loop the way you described then you have to write Windows service. Don't try to do it under ASP.NET application because ASP.NET applications are loaded and unloaded by web server. Web server decides when it is right time to stop application and when it is right time to wake it alive. 

    You can talk with your service by windows services API. Some useful links:

    The last link is good because it shows you how to impersonate your identity while communicating with Windows service.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 27, 2010 4:09 PM
  • User187056398 posted

    My wish is that there could be a default generic service installed on windows which would allow a web application to...
     

    There is, the Task Scheduler.  It has an API: you can even install tasks from the command line.

    http://msdn.microsoft.com/en-us/library/aa383608(v=VS.85).aspx

    Still, for security reasons, deploying on remote machines can be problematic.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2010 9:27 AM

All replies

  • User78900492 posted

    Instead of using a loop, why don't you use a timer object and implment the OnTick event.....You have check if a minute has past by taking the start time minus the end time, if it equals to 60 seconds, then fire you update to the text file. 

    Friday, March 26, 2010 11:48 PM
  • User1373395183 posted

    Good idea, thanks.


    I'm really hoping someone has an idea to improve on the 2 program design (service + ASP app).  Ideas?

    Friday, March 26, 2010 11:59 PM
  • User-525215917 posted

    I want to write a program that is running most of the time in an endless loop. Every 1 minute it writes the date and time to a text file on the hard drive. The program needs to have a web based front end which is hosted on the same machine, and allows me to stop or resume the loop from a web browser.

    When you have to do something in loop the way you described then you have to write Windows service. Don't try to do it under ASP.NET application because ASP.NET applications are loaded and unloaded by web server. Web server decides when it is right time to stop application and when it is right time to wake it alive. 

    You can talk with your service by windows services API. Some useful links:

    The last link is good because it shows you how to impersonate your identity while communicating with Windows service.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 27, 2010 4:09 PM
  • User-952121411 posted

    Take a look to the following article; it will help you create the Windows Service to work with running a process repeatedly on a set interval (i.e. 60 seconds or whatever you need):

    The link below details all you will need to get this task completed, and includes a link to a great easy to use code example.

    Running a Periodic Process in .NET using a Windows Service:

    http://allen-conway-dotnet.blogspot.com/2009/12/running-periodic-process-in-net-using.html

    A Windows Service is a great tool to run a long running background process as you have required.  Starting and stopping the service can be done on the server it was installed via the 'Services' mmc.  However, I think you can inject logic to control when you want the logic applied, and when not to apply it within the service, so you can basically start the service and not worry about stopping it manually.

    Hope this helps! Smile

     

    Monday, March 29, 2010 11:02 AM
  • User1621011946 posted

    Hi Guys,

    I have been looking at this kind of issue before. And, yes, there are situation where your web application needs to do some background work at specific intervals or at a specific time in the future. Now let me give my comment on the matter.

    Unfortunately there is no straight way to do it - at least so far I haven't see it. The problems I see here are:

    • designing and deploying a service its not that easy
    • it might be impossible if you can't put your hands on the server
    • you might want to keep you business logic all in one place - not necessarily on the only web application - but at least on your business logic project in your solution

    Some solutions I saw around rely on having a web service on your application which would be called by the windows service. But yet you might not want to force your web application to stay alive in the IIS and therefore using resources  because the windows service is calling - say - every minute just to do a check if anything needs to run.

    My wish is that there could be a default generic service installed on windows which would allow a web application to register to and specify the code to run accordingly pointing directly on one dll - maybe a specific project designed for the purpose of interact with the windows service but, yet, built in the web solution, therefore easy to maintain and to deploy.

    Thanks.
    Lorenzo.

    Tuesday, March 30, 2010 5:07 AM
  • User187056398 posted

    My wish is that there could be a default generic service installed on windows which would allow a web application to...
     

    There is, the Task Scheduler.  It has an API: you can even install tasks from the command line.

    http://msdn.microsoft.com/en-us/library/aa383608(v=VS.85).aspx

    Still, for security reasons, deploying on remote machines can be problematic.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 30, 2010 9:27 AM
  • User-952121411 posted

    designing and deploying a service its not that easy
     

    I do not agree much at all with the above comment.  Deploying a Windows Service is not a big deal at all.  Try the following walk-through from the MSDN if you have never done it before; it is much easier than most probably think, and you could probably do it in an hour on a test server:

    Walkthrough: Creating a Windows Service Application in the Component Designer:

    http://msdn.microsoft.com/en-us/library/zt39148a.aspx

    it might be impossible if you can't put your hands on the server

    This I do agree with; if you do not have access to the server, then installing a service is probably not an option.

    you might want to keep you business logic all in one place - not necessarily on the only web application - but at least on your business logic project in your solution

    Yes, in this case as you partially eluded to, you can farm the logic to a WCF service that the Windows Service can call.  You would only use the Windows Service as the 'worker' to make the calls on a determined interval.

     

    Tuesday, March 30, 2010 9:45 AM
  • User1621011946 posted

    Oops...

    thanks guys! 

    Tuesday, March 30, 2010 9:53 AM