Windows Serivce服務實現過程和打包安裝

來源:互聯網
上載者:User

標籤:top   啟動方式   override   system   pre   conf   名稱   ref   base   

 

 

 

環境:vs2013,win7

參考:

Windows服務的快速搭建與調試(C#圖解)

.Net實現Windows服務安裝完成後自動啟動的兩種方法

C# 產生windows 服務打包程式

1、建立項目Windows Service項目。

2、服務安裝後自動啟動。

右鍵添加安裝程式

要設定服務的一些屬性。設定運行服務的帳號類型為本地賬戶。

設定服務名稱,設定啟動方式為自動啟動。

注意:Display Name:是設定看到的服務名稱。在電腦》管理》服務,看到的是這個。

Service Name :是設定實際的服務名稱。在Window 工作管理員裡面看到的是這個。

在工作管理員進程裡看到的是Windows Service項目的名稱。

 

在ProjectInstaller類中,覆蓋父類Commit方法

代碼如下:注意名稱,此名稱要與Windows Service設定的名稱一樣,在serviceInstaller1右鍵屬性裡面設定

 

 1   [RunInstaller(true)] 2     public partial class ProjectInstaller : System.Configuration.Install.Installer 3     { 4         public ProjectInstaller() 5         { 6             InitializeComponent(); 7         } 8  9         public override void Commit(IDictionary savedState)10         {11             base.Commit(savedState);12             ServiceController sc = new ServiceController("Service1");13             if (sc.Status.Equals(ServiceControllerStatus.Stopped))14             {15                 sc.Start();16             }17         }18     }

 3、打包為exe檔案。

vs2013預設沒有打包項目,需要下載安裝。

Microsoft Visual Studio 2013 Installer Projects

安裝完後,解決方案右鍵添加新項目,Setup Project

 

 

Add ,項目輸出

選擇你的Windows service項目

在打包項目右鍵,view,自訂動作

每個都添加 自訂動作

 

最終效果

 最後,就可以產生安裝包了,在bin目錄下Debug中

 

 安裝完成後就可看到服務了

 

Windows Serivce服務實現過程和打包安裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.