4

私は、Webサービスを呼び出すWindowsサービスを作成しました。テストアプリからWindowsサービスを実行すると、完全に機能します。ただし、サービスをインストールして開始すると、すぐに停止します。ログに表示されるエントリは、 コンストラクタースレッド開始の2つだけです。何が悪いのかわからない。

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 に答える