Windows Service Flashcards

(6 cards)

1
Q

Why use a console application instead of the windows service project type?

A

Because we can run it at a console application and debug it locally and then install it as a service (instead of attaching the debugger to the running application)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What open source tool can you use?

A

Topshelf (right click reference, manage nuget packages)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What to add in the service class?

A
In the class (heartbeat)
Define a timer from system.timer
Private readonly Timer _timer;
Constructor:
Public heartbeat()
_timer=new Timer(1000){AutoReset=true};
Create event: (shortcut:+=tab)
_timer.elapsed+=Timerelapsed;
...
Public void Start()
{_timer.Start();}...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

TimerElapsed function that add the time to a text file

A

Private void TimerElapsed(object sender, elapsedeventargs e)
{
String[] lines=new string[]{DateTime.Now.ToString};
file.AppendAllLines(@“c:/temp/myFile.txt”, lines)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Code in main (using topshelf)

A

Var exitCode= HostFactory.run(x=>
{
x.Service(s=>
{
s.ConstructUsing(heartbeat=new HeartBeat());
s.WhenStarted(heartbeat=>heartbeat.start());
s.WhenStopped(heartbeat=>heartbeat.stop());
});
x.RunAsLocalSystem();
x.SetServiceName=…displayname,description
});
Int exitCodeValue= (int)convert.ChangeType(exitCode,exitCode.getTypeCode());
Environment.ExitCode=exitCode.value;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to install the windows service (from the console app)

A

Right click on the project-> openfolder in file explorer-> bin->debug, copy all file and put them in the folder you want. Copy the path, open admin command prompt, type:
Cd mypath
Myservice.exe install start
(Put uninstall instead to uninstall)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly