I have build an application to parse Xml file for integrating data in mssql database. I'm using Visual c# express. There's a way to make service with express edition or I a have to get Visual Studio to do it?
Absolutely you can. You can even do it with csc. The only thing in VS is the template. But you can reference the System.ServiceProcess.dll yourself.
Key points:
ServiceBase
Main(), use ServiceBase.Run(yourService)
ServiceBase.OnStart override, spawn whatever new thread etc you need to do the work (Main() needs to exit promptly or it counts as a failed start)Very basic template code would be:
Program.cs:
using System;
using System.ServiceProcess;
namespace Cron
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
System.ServiceProcess.ServiceBase.Run(new CronService());
}
}
}
CronService.cs:
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
}
}
}
CronInstaller.cs:
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);
}
}
And a .NET service application is not installed the same way as normal service application (i.e. you can't use cron.exe /install or some other command line argument. Instead you must use the .NET SDK's InstallUtil:
InstallUtil /LogToConsole=true cron.exe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With