[原]建立windows服務

來源:互聯網
上載者:User

1、建立windows服務工程MyService。
2、完成MyService類中的OnStart和OnStop方法,實現服務的啟動與停止。
註:建議該部分處理內容放在單獨的工程中,以後調試windows服務時,可以直接允許那個獨立工程而不用通過windows服務。
例如獨立對象使用:public static readonly ServiceProcesser ProcesserInstance = newServcieProcesser.ServiceProcesser();調用時使用:ServcieProcesser.ServiceProcesser.ProcesserInstance.OnStart();

3、添加安裝程式類,在項目上右鍵->添加->建立項->模板中選擇“安裝程式類”:MyServiceInstaller.
4、在建構函式中增加以下內容:

            // 建立ServiceProcessInstaller對象和ServiceInstaller對象
            System.ServiceProcess.ServiceProcessInstaller spInstaller =
                new System.ServiceProcess.ServiceProcessInstaller();
            System.ServiceProcess.ServiceInstaller sInstaller =
                new System.ServiceProcess.ServiceInstaller();

            // 設定ServiceProcessInstaller對象的帳號、使用者名稱和密碼等資訊
            spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            //this.spInstaller.Username = null;
            //this.spInstaller.Password = null;

            // 設定服務名稱
            sInstaller.ServiceName = "MyService";
            sInstaller.Description = "***服務名稱";

            // 設定服務的啟動方式
            sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

            Installers.AddRange(
                new System.Configuration.Install.Installer[] { spInstaller, sInstaller });
5、產生工程,完成服務工程。
6、通過批處理安裝:bat檔案中寫如下內容。
installutil FaisEtuService.exe
通過批處理卸載:bat檔案中:
installutil /u FaisEtuService.exe
【注】:InstallUtil.exe,InstallUtilLib.dll

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.