2016-01-20 121 views
0

我試圖發送自定義命令的Windows服務。我的應用程序有一個窗口服務和一個窗體。當服務運行時,我想使用按鈕單擊事件在獨立存儲中創建一個文件。我正在使用本教程http://arcanecode.com/2007/05/30/windows-services-in-c-sending-commands-to-your-windows-service-part-7/。當我運行該程序並單擊該按鈕時似乎沒有任何事情發生,並且該文件未放置到獨立存儲中。我已經在這些方法上設置了斷點,以查看它們是否執行並且它們似乎有效。我不確定按鈕點擊方法是否可以從表單中的服務中獲得隔離存儲代碼,但根據本教程我看不出問題會是什麼。下面的源代碼。發送命令到Windows服務

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using Project2Service; 

namespace Project2 
{ 
    public partial class Form1 : Form 
    { 
     public Service1 s = new Service1(); 
     public ServiceInstaller si = new ServiceInstaller(); 
     public ProjectInstaller pi = new ProjectInstaller(); 
     public ServiceController sc = new ServiceController("Project2Service"); 

     private string[] isoType; 

     string machineName = System.Windows.Forms.SystemInformation.ComputerName; 

     public Form1() 
     { 
      InitializeComponent(); 

      isoType = new string[] { "User", "Assembly And Domain"}; 
      cboIsoType.Items.AddRange(isoType); 

      cboIsoType.SelectedIndex = 0; 

      btnContinue.Enabled = false; 
      btnPause.Enabled = false; 
      btnStop.Enabled = false; 
     } 

     public void Labels() 
     { 
      lblMachine.Text = machineName; 
      lblSName.Text = s.ServiceName; 
      lblSType.Text = si.StartType.ToString(); 

      lblSStatus.Text = sc.Status.ToString(); 
      lblPause.Text = sc.CanPauseAndContinue.ToString(); 
      lblShutdown.Text = sc.CanShutdown.ToString(); 
      lblStop.Text = sc.CanStop.ToString(); 
     } 

     private void btnStart_Click(object sender, EventArgs e) 
     { 
      //Controller.Refresh(); //Gets the current status of service 
      //if (Controller.Status == ServiceControllerStatus.Stopped) 
      //{ 
      // Controller.Start(); 
      //} 

      sc.Start(); 
      sc.WaitForStatus(ServiceControllerStatus.Running); 
      Labels(); 

      btnStart.Enabled = false; 
      btnContinue.Enabled = false; 
      btnStop.Enabled = true; 
      btnPause.Enabled = true; 

     } 

     private void btnStop_Click(object sender, EventArgs e) 
     { 
      sc.Stop(); 
      sc.WaitForStatus(ServiceControllerStatus.Stopped); 
      Labels(); 

      btnStart.Enabled = true; 
      btnContinue.Enabled = false; 
      btnPause.Enabled = false; 
      btnStop.Enabled = false; 
     } 

     private void btnPause_Click(object sender, EventArgs e) 
     { 
      sc.Pause(); 
      sc.WaitForStatus(ServiceControllerStatus.Paused); 
      Labels(); 

      btnPause.Enabled = false; 
      btnContinue.Enabled = true; 
      btnStart.Enabled = false; 
      btnStop.Enabled = true; 
     } 

     private void btnContinue_Click(object sender, EventArgs e) 
     { 
      sc.Continue(); 
      sc.WaitForStatus(ServiceControllerStatus.Running); 
      Labels(); 

      btnStop.Enabled = true; 
      btnStart.Enabled = false; 
      btnPause.Enabled = true; 
      btnContinue.Enabled = false; 
     } 

     private void btnSubmit_Click(object sender, EventArgs e) 
     { 
      ServiceController sc2 = new ServiceController("Project2Service"); 
      sc2.ExecuteCommand(200); 

      //if (cboIsoType.SelectedItem.ToString() == "User") 
      //{ 

      // sc.ExecuteCommand(128); 
      //} 
     } 
    } 
} 

服務

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.IO; 
using System.IO.IsolatedStorage; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Project2Service 
{ 
    public partial class Service1 : ServiceBase 
    { 
     public enum commands 
     { 
      LogIt = 200 
     } 

     //public enum ServiceCustomCommands { Command1 = 128, Command2 = 129 }; 
     //private IsolatedStorageScope iso; 

     public Service1() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 

      //iso = IsolatedStorageScope.User | IsolatedStorageScope.Domain; 
      FileSystemWatcher Watcher = new FileSystemWatcher(@"C:\Users\Martin\Desktop\Project2\ServiceTest"); 
      Watcher.EnableRaisingEvents = true; 
      Watcher.NotifyFilter = NotifyFilters.LastAccess 
         | NotifyFilters.LastWrite 
         | NotifyFilters.FileName 
         | NotifyFilters.DirectoryName; 

      Watcher.Changed += new FileSystemEventHandler(Watcher_Changed); 
      Watcher.Created += new FileSystemEventHandler(Watcher_Created); 
      Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted); 
      Watcher.Renamed += new RenamedEventHandler(Watcher_Renamed); 
      WriteServiceInfo("Service Started!"); 
     } 

     // This event is raised when a file is changed 
     private void Watcher_Changed(object sender, FileSystemEventArgs e) 
     { 
      WriteServiceInfo("File Changed!"); 

      DirectoryInfo d = new DirectoryInfo(@"C:\Users\Martin\Desktop\Project2\ServiceTest");//Assuming Watch is your Folder 
      FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files 
      string str = ""; 
      foreach (FileInfo file in Files) 
      { 
       str = str + ", " + file.Name; 
       str = str + ", " + file.LastWriteTime; 
       str = str + ", " + file.CreationTime; 
       str = str + ", " + file.Length; 

       WriteServiceInfo(file.Name); 
       WriteServiceInfo(file.LastWriteTime.ToString()); 
       WriteServiceInfo(file.CreationTime.ToString()); 
       WriteServiceInfo(file.Length.ToString()); 
      } 
     } 

     private void Watcher_Created(object sender, FileSystemEventArgs e) 
     { 
      WriteServiceInfo("File Created!"); 
     } 

     private void Watcher_Deleted(object sender, FileSystemEventArgs e) 
     { 
      WriteServiceInfo("File Deleted!"); 
     } 
     private void Watcher_Renamed(object sender, FileSystemEventArgs e) 
     { 
      WriteServiceInfo("File Renamed!"); 
     } 

     private void WriteServiceInfo(string info) 
     { 
      FileStream fs = new FileStream(@"C:\Users\Martin\Desktop\Project2\WindowsServiceLog.txt", 
           FileMode.OpenOrCreate, FileAccess.Write); 
      StreamWriter m_streamWriter = new StreamWriter(fs); 
      m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
      m_streamWriter.WriteLine(info + "\n"); 
      m_streamWriter.Flush(); 
      m_streamWriter.Close(); 
     } 

     protected override void OnStop() 
     { 
      WriteServiceInfo("Service Stopped!"); 

     } 

     protected override void OnCustomCommand(int command) 
     { 
      base.OnCustomCommand(command); 
      if (command == (int)commands.LogIt) 
      { 
       IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null); 
       IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("LaptopInfo.txt", FileMode.Append, FileAccess.Write, isoFile); 
       using (StreamWriter writer = new StreamWriter(isoStream)) 
       { 
        writer.WriteLine("Data"); 
       } 
      } 
     } 

     //protected override void OnCustomCommand(int command) 
     //{ 
     // switch ((ServiceCustomCommands)command) 
     // { 
     //  case ServiceCustomCommands.Command1: 
     //   //Command1 Implementation 
     //   IsolatedStorageFile isoFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null); 
     //   IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("LaptopInfo.txt", FileMode.Append, FileAccess.Write, isoFile); 
     //   using (StreamWriter writer = new StreamWriter(isoStream)) 
     //   { 
     //    writer.WriteLine("Data"); 
     //   } 
     //   //iso = IsolatedStorageScope.User; 
     //   break; 
     //  case ServiceCustomCommands.Command2: 
     //   //iso = IsolatedStorageScope.User | IsolatedStorageScope.Assembly; 
     //   break; 
     //  default: 
     //   break; 

     // } 
     //} 
    } 
} 
+1

我已經回答了這個在您的其他線程,讓我尋找任何新的信息在這裏 –

+0

給我你的項目adamt在微軟,我會檢查出來 –

+0

我將如何做到這一點@ AdamTuliper-MSFT –

回答

0

這裏有幾件事情: 1.檢查您的服務正在運行的帳戶。使用您的服務安裝程序,您可以指定。我測試你的確切方案(使用代碼)與系統,以確保我能在我的桌面上訪問文件爲日誌測試,它工作得很好。 2.限制測試用例不要記錄到孤立的,而是一個簡單的文本文件日誌。

我也貼,我用你的其他崗位代碼 - 所以讓我們打掃一下第一,並找出解決哪一個。

+0

我採取了這兩種情況並實施它們。該服務運行的帳戶是LocalSystem,我拿走了孤立的存儲部分,並只寫了一個文本文件WriteServiceInfo(「寫入隔離存儲的數據」),它就可以工作。將文件放入IsolatedStorage是個問題。自定義命令正在工作。 –

+0

Jusy好奇,系統權限和運行服務爲什麼你使用本地存儲,而不是一個正常的文件?此外,如果您必須使用獨立存儲,請嘗試將您的服務作爲特定帳戶運行。 –