7

I've run into a situation where I deployed a .Net (C#) service on a Win 2008R2 server. The service has a dependency on MSMQ. At shutdown it needs to send a couple of quick messages before terminating. This works fine with manually triggered OnStop() events, but when the server is shutting down and the SCM calls OnShutdown() I'm finding that MSMQ has already shutdown and my service can't properly cleanup. My service only needs 2-5 seconds to do it's work.

I understand (now) that service dependencies only apply to startup, so that isn't helping. I spent some time today trying to figure out how to register my service to accept the newly (since Vista) available SERVICE_ACCEPT_PRESHUTDOWN events and work with the PreShutDownOrder feature (http://blogs.technet.com/b/askperf/archive/2008/02/04/ws2008-service-shutdown-and-crash-handling.aspx), but this isn't supported in the ServiceBase as implemented in the framework as far as I can tell.

I went down the path of trying to manually set it using the SetServiceStatus() function, but it doesn't appear to 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 an error status of 13 when you call GetLastError();

Any ideas for how to hook into the preshutdown service events?

3
  • Error 13 is "The data is invalid". Are you sure you've filled in the SERVICE_STATUS structure properly? Commented Nov 11, 2011 at 9:15
  • Just as a followup, it is possible. I was able to make this work by creating my own ServiceBase from a decompiled copy of ServiceBase and adding the required handlers for preshutdown. Commented Apr 19, 2013 at 16:26
  • I am looking for a C example to subscribe to pre-shutdown can someone help please. My question got downvoted so I deleted it just now Commented Jan 20, 2021 at 4:08

2 Answers 2

2

OK then the other alternative is to create/get/buy a C++ service that registers for the PreShutdown stuff and stops/shuts down your .Net services in your required order (and waits for them to actually exit).

It could either have its own shutdown order list/graph or build it using the existing Windows start up dependencies (ignoring services that it should not manage).

0

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

5
  • There are workarounds, but this answer doesn't really address the question. The messages that need to be sent are critical to orderly shutdown of other processes, so saving the data and trying later is not an option. Commented Sep 15, 2011 at 21:37
  • I did not find anything. I think you cannot set the order of shutdown services and you cannot subscribe to close of msmq. Commented Sep 15, 2011 at 21:56
  • I agree that I can't influence MSMQ's shutdown, but maybe I can get mine notified of the shutdown first. If you look at the article that I linked to in the question, you will see that it is indeed possible (since Vista). The question is, how do you hook into this functionality from a .Net service. Commented Sep 16, 2011 at 7:02
  • Antoher workaround , you can check msmq from another service every 5 second if it's live , if it's not to send message to your service Commented Sep 16, 2011 at 11:42
  • Another solution is to subscribe to to get updates to the "System" Event Log. When MSMQ is shutting down , may be it will write to Event Log. When the computer is shutting down "The Event log service was stopped." will appear and than maybe message can be sent to MSMQ Commented Sep 18, 2011 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.