Groups | Blog | Home
all groups > dotnet jscript > october 2003 >

dotnet jscript : Creating Windows Service using JScript.NET


Harjit S. Batra
10/31/2003 10:36:36 AM
I am trying to create a windows service in JScript and am confused. I have
two files, SimpleService.JS and SimpleServiceInstaller.JS as follows:

SimpleService.JS
/------------------------------------------------------------------------
import System;
import System.ServiceProcess;
import System.Diagnostics;
import System.Timers;

class SimpleService extends ServiceBase
{
private var timer : Timer = null;

function SimpleService()
{
this.CanPauseAndContinue = true;
this.ServiceName = "SimpleService";
this.timer = new Timer();
this.timer.Interval = 1000;
this.timer.add_Elapsed(OnTimer);
}

protected override function OnStart(args : String[])
{
EventLog.WriteEntry("Simple Service started");
this.timer.Enabled = true;
}

protected override function OnStop()
{
EventLog.WriteEntry("Simple Service stopped");
this.timer.Enabled = false;
}

protected override function OnPause()
{
EventLog.WriteEntry("Simple Service paused");
this.timer.Enabled = false;
}

protected override function OnContinue()
{
EventLog.WriteEntry("Simple Service continued");
this.timer.Enabled = true;
}

function OnTimer(source : Object, e : ElapsedEventArgs)
{
EventLog.WriteEntry("Hello World from SimpleService!");
}
}

ServiceBase.Run(new SimpleService());
/------------------------------------------------------------------------

and SimpleServiceInstaller.JS
/------------------------------------------------------------------------
import System;
import System.Collections;
import System.ComponentModel;
import System.Configuration.Install;
import System.ServiceProcess;

public RunInstallerAttribute(true) class SimpleServiceInstaller extends
Installer
{
private var serviceInstaller : ServiceInstaller = null;
private var serviceProcessInstaller : ServiceProcessInstaller = null;

public function SimpleServiceInstaller()
{
serviceInstaller = new ServiceInstaller();
serviceInstaller.ServiceName = "SimpleService";
serviceInstaller.DisplayName = "Simple Service";
serviceInstaller.StartType = ServiceStartMode.Manual;
this.Installers.Add(serviceInstaller);

serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
this.Installers.Add(serviceProcessInstaller);
}
}
/------------------------------------------------------------------------

I compiled both of them in the following manner:
jsc /t:exe SimpleService.js
and
jsc /t:exe SimpleServiceInstaller.js

And then installed the installer with:
installutil SimpleServiceInstaller.exe

On examining the Services.MMC, I see the service "SimpleService" in there
but whenever I attempt to start the service I get the dialog "Could not
start the Simple Service service on Local Computer, Error 1053: ..."!

Can someone please tell me what am I doing wrong and how do I fix it?

What confuses me is how the Installer knows which executable to look into
for the implementation of the service - should either the service or the
installer be written as a package and referenced by the other and if so
which one? OR should one of them be written as a DLL (which one)? OR should
they both be part of the same package? In short, where is the link between
"serviceInstaller.ServiceName = "SimpleService";" in the Installer and
"this.ServiceName = "SimpleService";" in the Service?

Thanks,

Harjit S. Batra

Harjit S. Batra
10/31/2003 5:00:13 PM
OK Folks, I figured this one out, thanks to help from Steve Brown at
Microsoft.

My compile line should have looked like :
jsc /t:exe /out:SimpleService.exe SimpleService.js SimpleServiceInstaller.js

and therefore my install line should have read:
installutil SimpleService.exe

I was then able to start and stop the service with
net start SimpleService
and
net stop SimpleService

Once again, Thank You - Steve!

Harjit

[quoted text, click to view]

code_ronin NO[at]SPAM yahoo.com
11/13/2003 6:21:51 AM
AddThis Social Bookmark Button