custom exceptions, exception message box in custom service application c#

Darryl Hoar 116 Reputation points
2022-05-02T15:00:06.73+00:00

I am using Visual Studio Pro 2019, .NET Framework 4

I have created a custom service application. In that application I have created
custom exceptions for when the user has entered invalid values in the app.config file.

After a loaded key value is tested and found lacking, I want to throw the custom exception and
then gracefully, immediately exit the service.

The part that is giving me a headache is the ExitCode and the windows generated error message in the pop up that
occurs when I throw the exception.

The problem is that when I set ExitCode to a non zero value, windows interprets the code and displays a message based on that code.
I want windows to display a custom message for my custom exception.

I understand WHY windows is display the specific messages (saw a list of codes and their standard meaning). Just need to know how I can
get it to display my message based on my codes.

thanks for any help. And yes I've googled and read but didn't find a discussion. No I did not read every of the thousands of returns google
suggested.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
{count} votes

Accepted answer
  1. RLWA32 42,796 Reputation points
    2022-05-03T14:37:06.307+00:00

    The Windows API provides for setting a service specific exit code. The SetServiceStatus function can be called with a SERVICE_STATUS parameter with these settings-

    dwWin32ExitCode  
      
    The error code the service uses to report an error that occurs when it is starting or stopping. To return an error code specific to the service, the service must set this value to ERROR_SERVICE_SPECIFIC_ERROR to indicate that the dwServiceSpecificExitCode member contains the error code. The service should set this value to NO_ERROR when it is running and on normal termination.  
      
    dwServiceSpecificExitCode  
      
    A service-specific error code that the service returns when an error occurs while the service is starting or stopping. This value is ignored unless the dwWin32ExitCode member is set to ERROR_SERVICE_SPECIFIC_ERROR.  
    

    Unfortunately, it appears that the .Net ServiceBase class does not expose this capability. The relevant data members of the ServiceBase class are private and cannot be seen by a user's derived class. And even if P/Invoke was used to call the Windows API function the data members of the ServiceBase class would be unaffected.

    In any event, you could try using P/Invoke in a test to see if it might suit your purposes.

    The MessageBox displayed by the Service Control Manager for a service specific exit code looks like this -

    198488-customexitcode.png


1 additional answer

Sort by: Most helpful
  1. Darryl Hoar 116 Reputation points
    2022-05-03T12:34:53.917+00:00

    OK.
    Attaching screen shot.
    I 198524-exception.png

    In my service application I have defined custom exceptions that get thrown when certain conditions occurr.
    Before throwing the custom exception, I set ExitCode to a specific integer value (that I associate with the custom exception).
    When I throw the exception, the message window you see in the attached screen shot appears. Windows interprets my
    ExitCode I set to its understanding ie, If I set it to 2, windows looks in ITS table and see 2 is ERROR_FILE_NOT_FOUND and the message
    it shows states that.

    Ultimately, I want to be able to have the pop up contain information about my custom Exception. IE, ExitCode 2 is Invalid Database Name.

    0 comments No comments