Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I call base class' methods in Windows services?

When deriving from ServiceBase, should I call the methods of the base class, too?

protected override void OnStart(string[] args)
{
    //
    // The stuff I do when the service starts.
    //

    base.OnStart(args); // Do I need to call this?
}
like image 279
Şafak Gür Avatar asked Sep 18 '12 14:09

Şafak Gür


2 Answers

The short answer is yes, you should.

In this specific case, the base implementation of the OnStart method doesn't do anything significant, but that is an implementation detail which could change at any time. As a general practice, you should always call the base method unless you have a good reason not to.

like image 180
Tim Copenhaver Avatar answered Sep 28 '22 05:09

Tim Copenhaver


If you decompile the service base with ILSpy or similar, you will see, that OnStart, OnStop, etc. do nothing (at least in .NET 4.0/4.5).

But this behaviour could change some time, so there could be unwanted or unpredicted behaviour in future releases of .NET, if you do not call it.
I think it's a good practice to call those base.OnEvent()-Methods.

like image 39
TGlatzer Avatar answered Sep 28 '22 03:09

TGlatzer