Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 108825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:58:23+00:00 2026-05-11T01:58:23+00:00

I have a windows service that has a custom configuration section. In the configSectionHandler

  • 0

I have a windows service that has a custom configuration section. In the configSectionHandler class I am using attributes on the properties to validate the settings like this:

    //ProcessingSleepTime Property     [ConfigurationProperty('ProcessingSleepTime', DefaultValue = 1000, IsRequired = false)]     [IntegerValidator(MinValue = 5, MaxValue = 60000)]     public Int32 ProcessingSleepTime     {         get         {             if (this['ProcessingSleepTime'] == null)                 return 100;              return (Int32)this['ProcessingSleepTime'];         }         set         {             this['ProcessingSleepTime'] = value;         }     } 

If a value in the configuration file fails validation, a ConfigurationErrorsException is thrown. In a windows service this happens as it is trying to start and it’s really ugly (it offers to launch the debugger). How can I gracefully handle this error? I tried wrapping the OnStart method in a try/catch but it had no effect.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:58:23+00:00Added an answer on May 11, 2026 at 1:58 am

    Or better yet (as you might need multiple such a properties), using the code from @Ricardo Villiamil, create:

    int GetIntFromConfigSetting(string settingName, int defaultValue) {    int retValue = defaultValue;    if(this.ContainsKey(settingName))    {       int sleepInterval;       if(Int32.TryParse(this[settingName], out sleepInterval)       {          retValue = sleepInterval;       }    }    return retValue; } 

    Then use it from any property you need to.

    EDIT: actually, after re-reading the question once more, looks like this solves your problem only half-way, as if the value is out of the defined range, it will throw exception anyway.

    EDIT2: You can hook the AppDomain.UnhandledException event in the static ctor of your config section handler. The static ctor is ran before any instance or static member of a class are accessed, so it guarantees that you will intercept the exception even if the main method of your service is not yet called.

    And then, when you intercept and log the error, you can exit the service with some error code != 0 ( Environment.Exit(errorCode) ), so the service manager knows it failed, but not try to invoke a debugger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Windows service that has a number of threads that all need
I have created a Windows Service that has a function that creates a new
We have a windows service that has been happily running in production for over
I have a windows service that runs various system monitoring operations. However, when running
I have a windows service that runs fine, but I have to have it
I have a Windows service that runs implementations of a framework across multiple threads.
I have written a Windows service that allows me to remotely run and stop
I have a dotnet windows service that's currently hung, but running. Is there anyway
I have written a Windows service that spawns a separate process. This process creates
I have a Deplhi based Windows Service that, on installation, parses some command line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.