4

我编写了一个 Windows 服务,它又调用了一个 Web 服务。当我从测试应用程序运行 Windows 服务时,它运行良好。但是,当我安装该服务然后启动它时,它几乎立即停止。我在日志中看到的仅有的两个条目是 ConstructorThread Started。不知道出了什么问题。

public partial class WindowsService : ServiceBase
{
    public LogManager.LogFile _log;
    public Thread m_thread;
    protected TimeSpan m_delay;

    CommonFunctions _cf = new CommonFunctions();
    DBFunctions _db = new DBFunctions();

    public WindowsService()
    {
        InitializeComponent();
        _log = new LogManager.LogFile(@"c:\test\servicelog.txt", true, true);
        _log.WriteToLog("Constructor", LogLevel.Level0);
    }


    protected override void OnStart(string[] args)
    {

       m_delay = new TimeSpan(0,0,300);
       base.OnStart(args);

        try
        {
            m_thread = new System.Threading.Thread(Execute);
            m_thread.Start();
            _log.WriteToLog("Thread Started", LogLevel.Level0);
        }
        catch (Exception ex)
        { _log.WriteToLog(ex.Message, LogLevel.Level0); }

    }

  public void Execute()
    {
        _log.WriteToLog("Begin Execute...", LogLevel.Level0);

        try
        {

            ProcessNewLMSUsers();

         }
        catch (Exception ex)
        {
             _log.WriteToLog(ex.Message.ToString());
        }

     }

    private void ProcessNewLMSUsers()
    {
        try
        {
            _log.WriteToLog("Begin: Processing new LMS Users", LogLevel.Level1);

            // Check for new users in the LMS.
            string callErrorText = "";
            bool userAdded = false;

            LMSWS.SSO lms = _cf.GetLMSSSOWS(); **// this is a web service**
            lms.Timeout = 99999;


          }

             REST OF THE CODE.................
     }
4

1 回答 1

0

我看不出你的代码有什么问题。但您可以尝试在 OnStart 方法的开头添加“Thread.Sleep(20000);”代码。例如

protected override void OnStart(string[] args)
{
   Thread.Sleep(20000); 

   m_delay = new TimeSpan(0,0,300); //set a break-point here
   base.OnStart(args);

    try
    {
        m_thread = new System.Threading.Thread(Execute);
        m_thread.Start();
        _log.WriteToLog("Thread Started", LogLevel.Level0);
    }
    catch (Exception ex)
    { _log.WriteToLog(ex.Message, LogLevel.Level0); }

}

一旦你在 Windows 服务中启动了这个服务程序,那么你必须快速将你的源代码附加到服务程序中。在 Visual Studio 中,它是菜单“调试”->“附加到进程...”。然后你可以在源代码的任何地方设置断点来检查出了什么问题。

于 2012-11-23T02:59:09.017 回答