2014-09-25 87 views
1

我想在Windows服務啓動後將一些字符串寫入文本文件,但啓動後它沒有任何響應。我的代碼有什麼問題?C#Windows服務啓動後未響應

WindowsService.cs

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Diagnostics; 
    using System.Linq; 
    using System.ServiceProcess; 
    using System.Text; 
    using System.IO; 
    using System.Threading.Tasks; 
    //using System.Threading; 

    namespace TOU_Transference_Service 
    { 
     public partial class WindowsService : ServiceBase 
     { 
      public WindowsService() 
      { 
       InitializeComponent(); 
       this.ServiceName = "TOUTransference"; 
       this.EventLog.Log = "Application"; 

       // These Flags set whether or not to handle that specific 
       // type of event. Set to true if you need it, false otherwise. 
       this.CanHandlePowerEvent = true; 
       this.CanHandleSessionChangeEvent = true; 
       this.CanPauseAndContinue = true; 
       this.CanShutdown = true; 
       this.CanStop = true; 
      } 
      System.Threading.Timer TimerItem; 


      /// <summary> 
      /// OnStart(): Put startup code here 
      /// - Start threads, get inital data, etc. 
      /// </summary> 
      /// <param name="args"></param> 
      protected override void OnStart(string[] args) 
      { 
       try 
       { 
        ServiceController service = new ServiceController("TOUTransference", "."); 
        if (service.Status == ServiceControllerStatus.Running) 
         WriteLog("Process Started"); 
        base.OnStart(args); 
       } 
       catch (Exception err) 
       { 
        throw err; 
       } 
      } 

      /// <summary> 
      /// OnStop(): Put your stop code here 
      /// - Stop threads, set final data, etc. 
      /// </summary> 
      protected override void OnStop() 
      { 
       try 
       { 
        ServiceController service = new ServiceController("TOUTransference", "."); 
        if (service.Status == ServiceControllerStatus.Stopped) 
         WriteLog("Process Stopped"); 
        base.OnStop(); 
       } 
       catch (Exception err) 
       { 
        throw err; 
       } 
      } 
     private void WriteLog(string text) 
     { 
      try 
      { 
       StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test.txt", true); 
       sw.WriteLine(DateTime.Now.ToString() + " : " + text + "\n"); 
       sw.Close(); 
      } 
      catch (Exception err) 
      { 
       throw err; 
      } 
     } 
    } 
} 

WindowsServiceInstaller.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Configuration.Install; 
using System.ServiceProcess; 
using System.ComponentModel; 

namespace TOU_Transference_Service 
{ 
    [RunInstaller(true)] 
    public class WindowsServiceInstaller : Installer 
    { 
     /// <summary> 
     /// Public Constructor for WindowsServiceInstaller. 
     /// - Put all of your Initialization code here. 
     /// </summary> 
     public WindowsServiceInstaller() 
     { 
      ServiceProcessInstaller serviceProcessInstaller = 
           new ServiceProcessInstaller(); 
      ServiceInstaller serviceInstaller = new ServiceInstaller(); 

      //# Service Account Information 
      serviceProcessInstaller.Account = ServiceAccount.LocalSystem; 
      serviceProcessInstaller.Username = null; 
      serviceProcessInstaller.Password = null; 

      //# Service Information 
      serviceInstaller.DisplayName = "TOU Transference"; 
      serviceInstaller.StartType = ServiceStartMode.Automatic; 

      //# This must be identical to the WindowsService.ServiceBase name 
      //# set in the constructor of WindowsService.cs 
      serviceInstaller.ServiceName = "TOUTransference"; 

      this.Installers.Add(serviceProcessInstaller); 
      this.Installers.Add(serviceInstaller); 
     } 

     private void InitializeComponent() 
     { 

     } 
    } 
} 

的Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Threading.Tasks; 

namespace TOU_Transference_Service 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     static void Main() 
     { 
      ServiceBase[] ServicesToRun; 
      ServicesToRun = new ServiceBase[] 
      { 
       new WindowsService() 
      }; 
      ServiceBase.Run(ServicesToRun); 
     } 
    } 
} 
+0

並呼籲WRITELOG不檢查服務狀態的工作? – Arie 2014-09-25 13:46:31

+0

是的,它有或沒有狀態檢查工作,但只有初始狀態,如果想檢查它 – Peter 2014-09-27 03:54:51

回答

1

這是你的文本文件

文件夾「Environment.SpecialFolder.Desktop」是不一樣的桌面文件夾中,如果服務以LocalSystem運行的位置。

您可以:

  • 將文件中的一個地方,是獨立於執行用戶的(如C:\ ..),或硬編碼到用戶的桌面文件夾(C物理路徑:\ Users \ [用戶提供yourname] \桌面\ ...)。硬編碼是一個不好的做法。
  • 修改服務,使其運行作爲自己的用戶
+0

謝謝,這是工作時,我修改硬代碼到桌面,因爲「Environment.SpecialFolder.Desktop」是「C:\ Windows \ system32 \ config \ systemprofile \桌面」它是未知的位置。 – Peter 2014-09-26 06:12:20

2

首先,要檢查if (service.Status == ServiceControllerStatus.Running)當你確實應該檢查if (service.Status == ServiceControllerStatus.StartPending)爲你的避風港尚未完成開始。

其次,請確保您正在運行該服務的用戶(無論是本地系統還是特定用戶)有權編輯您要將文件寫入的文件夾。

+0

謝謝,它的工作時,改變檢查.StartPending狀態或不檢查。 – Peter 2014-09-26 06:16:02