C# Windows Service Timeout on startup

后端 未结 5 438
小鲜肉
小鲜肉 2021-02-05 11:41

I\'m having difficulty trying to determine the cause of a timeout in a Windows Service I\'ve created with C#. I\'ve spent a considerable amount of time looking at several posts

相关标签:
5条回答
  • 2021-02-05 12:12

    I fixed similar issue by turning off publisher evidence generation in config file. The service also did not have authenticode signing but adding following line immediately fixed the issue on the machine where it has been reproduced consistently.

    <runtime>
        <generatePublisherEvidence enabled="false" />
    </runtime>
    

    Also recommended in this MSDN source:
    "We recommend that services use the element to improve startup performance. Using this element can also help avoid delays that can cause a time-out and the cancellation of the service startup. "

    0 讨论(0)
  • 2021-02-05 12:12

    Since log4net is not designed to be (in their words) a reliable logging system, I thought it was good practice to write unhandled exceptions to the eventlog (as well as to your log), especially with services.

    0 讨论(0)
  • 2021-02-05 12:21

    In general, spawning a background thread from OnStart is the right thing to do.

    For troubleshooting purposes, you could try to give your service more startup time by calling RequestAdditionalTime method from OnStart. Also, you might want to check if any messages have been written to the Windows EventLog (log "Application", the source should be your service name).

    0 讨论(0)
  • 2021-02-05 12:27

    A few things to try:

    • Add log messages to the top of Main(), before ServiceBase.Run(), etc. Assuming you get a log file, how do those timestamps compare to the Windows Event Log?

    • Create a brand new Service with the New Project Wizard and deploy it as-is. On the problem machines, does it start reliably?

    • Get process monitor and watch a normal startup. Look for any unexpected network or file I/O.

    • Make sure your SchedulerService does not do any work in the constructor, and does not have any statically-initialized dependencies.

    • Set the Recovery options to restart on first failure. Does that work reliably?

    0 讨论(0)
  • 2021-02-05 12:27

    I'd also suspect that log4net is somehow hanging. Maybe the drive where the log is to be created is not ready yet when the machine is booting. Have you tried starting your service delayed?

    enter image description here

    0 讨论(0)
提交回复
热议问题