简体   繁体   中英

.NET Windows service working only with debug binaries

I have installed a Windows service developed in c#. The service has been installed by an msi package created by a visual studio installer project. When the service is installed with binaries coming from a release build it fails to start and the service console returns the following error message:"Error 1053: The service did not respond to the start or control request in a timely fashion". When the service is installed with debug binaries everything works as expected. I have added logs for every exception that occurs in the service's constructor, in the OnStart method, in the main method, and in the DoWork method of the main working thread of the service. The OnStart method of the service exits almost immediatly as it just initializes some variables from the configuration file and then starts the main working thread. I get no exception logged on the event viewer. I am clueless, how could I debug what is happening with the release binaries? Thanks.

I would change the code to enable the service to run from a standalone console test harness. After all a service is just a fancy way managing start shut down and pause/resume. Then debug your code out side of being a service.

If that fails, then break the code into multiple assemblies and see if you can isolate the code module more precisely. Then try recreate the issue by logging the entry conditions to the affected module, and recreating them in a console test harness

Edit: Just to chime in. 99% of services issues in complex enterprise service code I've debugged has been solved using this pattern. The rest where security issues which @LexLi is right can't always solved by running the app as a console application.

Also my answer to all debugging issues is generally in this pattern:

  1. Iteratively reduce systems (environment) complexity
  2. Iteratively remove code (under test) complexity
  3. Repeat until the problem be reproduced and then apply either source code or runtime debuggers, either custom or off the self.

You can try to remove MSI from the process so as to better identify the root cause.

  1. Compile your project in Release build.
  2. Use InstallUtil to install the Release build binary as a Windows service. http://msdn.microsoft.com/en-us/library/sd8zc8ha(v=vs.80).aspx

Try to start this service and see if the same error message occurs.

It the service still fails to start, try to follow KB to set up service debugging,

http://support.microsoft.com/kb/824344

As this is a .NET Windows service, you might learn about WinDbg and SOS,

http://www.netfxharmonics.com/2009/01/Learning-WinDBGSOS-and-Advanced-Debugging

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM