none
Service Fails to start whenever I have code inside it to access app.config using configuration manager RRS feed

  • Question

  • Hello,

    I have coded a windows service. It runs, starts, stops or works fine. But I am using hard coded configurable values in the code, which is not a good practice. I would rather use the App.config file. Except, I am unable to. Whenever I modify the App.config, for example as shown below..

    <configuration>

      <appSettings>

        <add key="production" value="y"/>

      </appSettings>

    and then in my source code files try to access the configuration setting, for example with code

    if (ConfigurationManager.AppSettings["production"] == "y")

    my service fails to start. It gives me the following error message, when I try to start it..

    "Windows could not start the <ServiceNameHere< service on Local Computer

    Error 1053: The service did not respond to the start or control request in a timely fashion."

    Please note I am able to compile & install the service (using installutil command), just fine. The problem is that it never starts.

    Help Appreciated. thank you


    diana4

    Sunday, June 14, 2015 1:09 AM

Answers

  • Hello Diana4,

    According to your description, I just created a small test which reads a value from the app.config and write it to a file as below in the OnStart method:

    protected override void OnStart(string[] args)
    
            {
    
                try
    
                {
    
                    string value = ConfigurationManager.AppSettings["production"];
    
    
                    if (ConfigurationManager.AppSettings["production"] == "y")
    
                    {
    
                        File.AppendAllText(@"D:\BMX\Project\2015\CLRs\WindowsService20150615\bin\Debug\Log.txt", value);
    
                    }
    
                    
    
                }
    
                catch (Exception ex)
    
                {
    
                    File.AppendAllText(@"D:\BMX\Project\2015\CLRs\WindowsService20150615\bin\Debug\Log.txt", ex.ToString());
    
    
                    File.AppendAllText(@"D:\BMX\Project\2015\CLRs\WindowsService20150615\bin\Debug\Log.txt", ex.Message);
    
                }
    
                
    
            }
    

    However, it worked fine, the value is written to the Log.txt file successfully. My detail environment is win8.1, .NET 4.5 and VS2013, I built the service according to this link:

    https://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    You could try above code and similar environment or share yours with us. And although it is a windows service, there is still a approach to debug it:

    Debugging Windows Services under Visual Studio .NET

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 15, 2015 6:27 AM
    Moderator

All replies

  • Hello Diana4,

    According to your description, I just created a small test which reads a value from the app.config and write it to a file as below in the OnStart method:

    protected override void OnStart(string[] args)
    
            {
    
                try
    
                {
    
                    string value = ConfigurationManager.AppSettings["production"];
    
    
                    if (ConfigurationManager.AppSettings["production"] == "y")
    
                    {
    
                        File.AppendAllText(@"D:\BMX\Project\2015\CLRs\WindowsService20150615\bin\Debug\Log.txt", value);
    
                    }
    
                    
    
                }
    
                catch (Exception ex)
    
                {
    
                    File.AppendAllText(@"D:\BMX\Project\2015\CLRs\WindowsService20150615\bin\Debug\Log.txt", ex.ToString());
    
    
                    File.AppendAllText(@"D:\BMX\Project\2015\CLRs\WindowsService20150615\bin\Debug\Log.txt", ex.Message);
    
                }
    
                
    
            }
    

    However, it worked fine, the value is written to the Log.txt file successfully. My detail environment is win8.1, .NET 4.5 and VS2013, I built the service according to this link:

    https://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    You could try above code and similar environment or share yours with us. And although it is a windows service, there is still a approach to debug it:

    Debugging Windows Services under Visual Studio .NET

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 15, 2015 6:27 AM
    Moderator
  • Thank you. I am using Windows 7, visual studio 2012.  I am going to try to debug this using the link you have in your email.

    I am wondering if my installation method somehow did not copy the app.config to proper location. I just did a standard install using installutil. Maybe it is not finding the app.config, at startup time


    diana4

    Monday, June 15, 2015 3:44 PM

  • Hello diana4,

    Any update? I have marked my reply as answer since I think it is helpful, if you think it provides no help, please unmark it.

    Thank you for your understanding and support.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 23, 2015 8:25 AM
    Moderator