2014-09-25 141 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