Is it possible to register for PreShutdown service events using .Net?

I came across a situation when I deployed the .NET service (C #) on a Win 2008R2 server. The service is dependent on MSMQ. When completing work, you must send some quick messages before sending. This works fine with manually triggered OnStop () events, but when the server shuts down and SCM calls OnShutdown (), I find that MSMQ is already turned off and my service cannot properly clear. For this I need only 2-5 seconds.

I understand (now) that service dependencies only apply to startup, so this does not help. Today I spent some time figuring out how to register my service, to accept new (starting from Vista) SERVICE_ACCEPT_PRESHUTDOWN events and work with the PreShutDownOrder function ( http://blogs.technet.com/b/askperf/archive/2008/ 02/04 / ws2008-service-shutdown-and-crash-handling.aspx ), but this is not supported in ServiceBase, as it is implemented in the structure, as far as I can tell.

I went down the path to manually set it using the SetServiceStatus () function, but it does not work.

_serviceHandle = this.ServiceHandle; SERVICE_STATUS serviceStatus = new SERVICE_STATUS(); serviceStatus.currentState = (int)State.SERVICE_RUNNING; serviceStatus.controlsAccepted = (int)(ControlsAccepted.SERVICE_ACCEPT_PRESHUTDOWN | ControlsAccepted.SERVICE_ACCEPT_STOP); serviceStatus.waitHint = 0; serviceStatus.checkPoint = 0; bool setStatus = SetServiceStatus(_serviceHandle, ref serviceStatus); int error = Marshal.GetLastWin32Error(); 

This returns error status 13 when calling GetLastError();

Any ideas on how to connect to pre-launch events?

+6
source share
5 answers

OK, then another alternative is to create / receive / buy a C ++ service that registers for PreShutdown material and stops / turns off your .Net services in the required order (and waits for them to exit).

It can either have its own shutdown order list / schedule, or build it using existing Windows startup dependencies (ignoring services that it should not manage).

+2
source

If you write a message to the database / file when the service closes and than when the computer again, check the table / file and send the message to MSMQ

0
source

This article provides some basic ServiceModel components for registering with PreShutDown ... - http://www.sivachandran.in/2012/03/handling-pre-shutdown-notification-in-c.html

However, it refers to a private variable, and therefore any person I have seen implement this, throw an exception (as this article demonstrates) if the updated acceptCommands member changes in later versions of the .NET Framework.

I’m more interested in knowing if someone manually entered the value of their processes in the registry entry HKLM \ SYSTEM \ CurrentControlSet \ Control \ PreShutDownOrder. Anyone try this?

0
source

Alternatively, you can use a more secure operating system, in which it is much easier to set the settings so that a normal shutdown does not lead to data loss.

Seriously, there are already service dependencies, so Windows should just use them if it is a routine shutdown, rather than “shutting down a low battery UPS”. And in this regard, the excuse that you are not completing a proper shutdown due to things such as “Low UPS” is silly when it is so simple and common for Windows that it results in 30+ windows updates on shutdown. ..

In any case, the worse news is here: http://connect.microsoft.com/VisualStudio/feedback/details/641737/add-windows-service-preshutdown

As for the suggestions to write the material elsewhere: 1) I thought that one of the reasons for using MSMQ is a more reliable messaging than just writing the material to a file. 2) AFAIK, you cannot write to the database either since Windows can shut down the database in front of your service without these dependencies. Heck without regard to dependencies, he is not even 100% sure that you can write to the desired file system at this moment

-1
source

Source: https://habr.com/ru/post/897425/


All Articles