Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Absolutely you can. You can even do it with <code>csc</code>. The only thing in VS is the <strong>template</strong>. But you can reference the System.ServiceProcess.dll yourself.</p> <p>Key points:</p> <ul> <li>write a class that inherits from <code>ServiceBase</code></li> <li>in your <code>Main()</code>, use <code>ServiceBase.Run(yourService)</code></li> <li>in the <code>ServiceBase.OnStart</code> override, spawn whatever new thread etc you need to do the work (<code>Main()</code> needs to exit promptly or it counts as a failed start)</li> </ul> <h2>Sample Code</h2> <p>Very basic template code would be:</p> <p><strong>Program.cs</strong>:</p> <pre><code>using System; using System.ServiceProcess; namespace Cron { static class Program { /// &lt;summary&gt; /// The main entry point for the application. /// &lt;/summary&gt; static void Main() { System.ServiceProcess.ServiceBase.Run(new CronService()); } } } </code></pre> <p><strong>CronService.cs</strong>: </p> <pre><code>using System; using System.ServiceProcess; namespace Cron { public class CronService : ServiceBase { public CronService() { this.ServiceName = "Cron"; this.CanStop = true; this.CanPauseAndContinue = false; this.AutoLog = true; } protected override void OnStart(string[] args) { // TODO: add startup stuff } protected override void OnStop() { // TODO: add shutdown stuff } } } </code></pre> <p><strong>CronInstaller.cs</strong>:</p> <pre><code>using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; [RunInstaller(true)] public class CronInstaller : Installer { private ServiceProcessInstaller processInstaller; private ServiceInstaller serviceInstaller; public CronInstaller() { processInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller(); processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.StartType = ServiceStartMode.Manual; serviceInstaller.ServiceName = "Cron"; //must match CronService.ServiceName Installers.Add(serviceInstaller); Installers.Add(processInstaller); } } </code></pre> <p>And a .NET service application is not installed the same way as normal service application (i.e. you can't use <code>cron.exe /install</code> or some other command line argument. Instead you must use the .NET SDK's <code>InstallUtil</code>:</p> <pre><code>InstallUtil /LogToConsole=true cron.exe </code></pre> <h2>Resources</h2> <ul> <li><a href="http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c6027/" rel="noreferrer">Creating a Windows Service in .NET</a> by Mark Strawmyer</li> <li><a href="http://blogs.msdn.com/b/bclteam/archive/2005/03/15/396428.aspx" rel="noreferrer">Writing a Useful Windows Service in .NET in Five Minutes</a> by Dave Fetterman</li> <li><a href="http://msdn.microsoft.com/en-us/library/aa984379%28v=vs.71%29.aspx" rel="noreferrer">Installing and Uninstalling Services</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/aa984464%28v=vs.71%29.aspx" rel="noreferrer">Walkthrough: Creating a Windows Service Application in the Component Designer</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload