question

DarrylHoar-5879 avatar image
0 Votes"
DarrylHoar-5879 asked RLWA32-6355 commented

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

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.

dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Have you considered providing a user interface to allow users to change values in app.config which does validation?

0 Votes 0 ·

No,
because this is a service application (launches from Windows Services).

Thanks.

0 Votes 0 ·

Can Please attach the screenshot of your problem. Sorry but it's hard to understand.

0 Votes 0 ·

I understand that you may not have received the desired response to your question. But regardless, has your question been answered?

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

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



customexitcode.png (6.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

By the way, you should use service specific exit codes that do not coincide with system error codes. The system issued message box directs a user to the System Event log. In the above example using code 2, the System Event log will contain an entry stating "The system cannot find the file specified" which would be contrary to your objective and confusing to a user. If a unique service specific exit code is used its numeric value will appear in the System Event log entry.

0 Votes 0 ·
DarrylHoar-5879 avatar image
0 Votes"
DarrylHoar-5879 answered

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.



exception.png (57.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.