2

I try for the first time to create a service. The service is started automatically as user.

I want to handle the shutdown to also do some cleanups.

For that I tried the following: In the service I set

CanShutdown to true. In the Service file I used the follwoing code:

    protected override void OnShutdown()
    {
        this.eventLog.WriteEntry("OnShutdown.");
        this.OnStop(); // does some cleanup
        base.OnShutdown();
    }

(where Eventlog is System.Diagnostic.EventLog which logs to Application (and is also used in OnStart and OnStop wchich write the message to eventlog).

So for me it looks like the OnShutdown is not triggered.

What can be the possible reason? (I see no possibility to debug during shutdown)

Offler
  • 1,223
  • 1
  • 12
  • 34
  • Change the account for the service to a domain account, the OnShutdown should be triggered. –  Feb 10 '15 at 12:57
  • There are only local accounts on the machine - it is not in a domain but in a workgroup. Username was provided for service installation as MachineName\UserName – Offler Feb 10 '15 at 13:10
  • Change the service to run then as Local Service –  Feb 10 '15 at 13:13
  • Does logging for the other events work as desired? – Codor Feb 10 '15 at 13:14
  • @codor logging the other events work as desired. – Offler Feb 10 '15 at 13:17
  • @NeillVerreynne I read that local service will have very limited access right - therefore i would choose the local user. I tried local service - no log entires for shutdown. Where do you find information that it should be triggered only for special accounts? – Offler Feb 10 '15 at 13:35
  • @Offler Windows has an option called fast startup which does not call `OnShutdown`. [Check here for details](https://stackoverflow.com/a/53783761/2923208) – rschoenbach Dec 14 '18 at 16:52
  • @rschoenbach i know what you mean, but this entry is a bit older and with W7 resp W2008R2 not 10... – Offler Dec 15 '18 at 20:34

2 Answers2

0

To answer the question in part, I is well possible to add a Windows Service. To do so, compile the service for "debug" to include debugging information. Then, install and start the service manually. While the service is running, in Visual Studio, select "attach to process" from the "debug menu" (or similar, I don't know the exact terminology). Beforehand, find out the process ID with the Task Manager (tab page "services").

Codor
  • 17,447
  • 9
  • 29
  • 56
  • Can he continue debugging while the system is shutting down? I think before stopping services, Windows closes applications, is that right? – Mert Akcakaya Feb 10 '15 at 13:01
  • I don't know, it seems to make sense. – Codor Feb 10 '15 at 13:07
  • My problem is that Visual Studio will be closed before the service closes - therefore i see atm no possibility to debug. – Offler Feb 10 '15 at 13:11
  • This does not work at all. Seems like a bug in Windows or the .NET framework. You can have it write to a file in OnShutdown. The file is never written. Even if you set CanShutdown = true in the constructor, it still does not work. – jjxtra Feb 19 '18 at 18:47
0

I had similar experiences and my findings were, that the OnShutDown event was triggered, but you cannot assume that any functions are working in OnShutDown, as you don't know which other services your functions are depending on are living, if the OnShutDown in your Service is called.
I ended up by writing my own flag files with low level functions.
All details here:
Weird behavior with onshutdown event

FredyWenger
  • 2,236
  • 2
  • 32
  • 36