I have seen the question of how to run a Lansa function as a Windows Service posted may times in various online forums. The Lansa executable (X_RUN.exe) does not have the entry and exit points for the Windows Service Controller. There are third-party application available for running an executable as a program, but they are not lightweight and they cost money. I solved this problem by creating a simple C# solution in Visual Studio 2010.
The Generic Windows Service Solution creates a process object with configurable command line arguments. In order to run a Lansa function as a service the command line is specified as the path to X_RUN.exe in the system executable folder and the command line arguments are the X_RUN arguments. The Lansa function can execute in a loop with a brief pause using the OV_SLEEP BIF. The Lansa function should not loop without a delay as the executing function will consume increasing system memory without giving the operating system the opportunity to reclaim resources. The service can be configured to either terminate when the process exits or restart the process if it is terminated.
The Generic Windows Service and/or Visual Studio 2010 Solution can be downloaded from the files section of the Yahoo Lansa User Group at http://tech.groups.yahoo.com/group/lansa2/files/. You must be a member of the group to download from the files section. The Windows Service is in the GenericService.zip file and includes batch files for installing and configuring the service.
Below is the C# source code for the Generic Windows Service solution:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Configuration;
namespace LansaService
{
public partial class LansaService : ServiceBase
{
Process LansaProc = new Process();
Timer LansaTimer = new Timer();
bool LansaRestart = true;
public LansaService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
LansaRestart = Convert.ToBoolean( ConfigurationSettings.AppSettings["Restart"].ToString());
startLansa();
}
private bool startLansa()
{
LansaTimer.Stop();
LansaProc.StartInfo.WorkingDirectory = ConfigurationSettings.AppSettings["WorkingDirectory"].ToString();
LansaProc.StartInfo.FileName = ConfigurationSettings.AppSettings["ExecutablePath"].ToString();
LansaProc.StartInfo.Arguments = ConfigurationSettings.AppSettings["Arguments"].ToString();
LansaProc.StartInfo.UseShellExecute = false;
LansaProc.StartInfo.RedirectStandardOutput = false;
LansaProc.StartInfo.RedirectStandardError = false;
if (LansaProc.Start())
{
LansaTimer.Interval = 5000;
LansaTimer.Elapsed += new ElapsedEventHandler(LansaTimer_Elapsed);
LansaTimer.Start();
return true;
}
else
{
OnStop();
return false;
}
}
void LansaTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (LansaProc.HasExited)
{
if (LansaRestart)
{
startLansa();
}
else
{
OnStop();
}
}
}
protected override void OnStop()
{
try
{
LansaProc.Kill();
}
catch (Exception)
{
}
}
}
}
