編寫Windows服務疑問2:探索服務與安裝器的關係

來源:互聯網
上載者:User

標籤:

首先,來弄兩個服務,一個叫“飛機”,一個叫“火車”。

    public class FeiJiService : ServiceBase    {        public FeiJiService()        {            ServiceName = "Fei_ji";        }    }    public class HuoCheService : ServiceBase    {        public HuoCheService()        {            ServiceName = "Huo_che";        }    }

 

用於示範,服務很單,接著,匹配安裝器。

    [RunInstaller(true)]    public class SelfInstaller : Installer    {        public SelfInstaller()        {            ServiceInstaller fjinstaller = new ServiceInstaller();            fjinstaller.ServiceName = "Fei_ji";            fjinstaller.Description = "國產飛機 -- 008";            fjinstaller.DisplayName = "飛機";            Installers.Add(fjinstaller);            ServiceProcessInstaller processinstaller = new ServiceProcessInstaller();            processinstaller.Account = ServiceAccount.LocalSystem;            Installers.Add(processinstaller);        }    }

這裡我搗了個鬼,只安裝了“飛機”服務,“火車”服務沒有安裝。

 

最後,偏偏在Main進入點處運行兩個服務。

        static void Main()        {            ServiceBase[] svs =            {                    new FeiJiService(),                    new HuoCheService()                };            ServiceBase.Run(svs);        }

 

咱們就是來驗證一下,沒有被安裝的服務到底能不能運行。

現在,執行installutil xxx.exe進行安裝,安裝後,在服務管理員中只看“飛機”,沒看到“火車”。

顯然,目前只能啟動“飛機”服務,而“火車”服務不在服務列表中。

看來,只有安裝後的服務才能啟動。

 

下面,再次修改安裝器代碼,把“火車”服務也安裝上。

        public SelfInstaller()        {            ServiceInstaller fjinstaller = new ServiceInstaller();            fjinstaller.ServiceName = "Fei_ji";            fjinstaller.Description = "國產飛機 -- 008";            fjinstaller.DisplayName = "飛機";            Installers.Add(fjinstaller);            ServiceInstaller hcinstaller = new ServiceInstaller();            hcinstaller.ServiceName = "Huo_che";            hcinstaller.Description = "國產列車";            hcinstaller.DisplayName = "火車";            Installers.Add(hcinstaller);            ServiceProcessInstaller processinstaller = new ServiceProcessInstaller();            processinstaller.Account = ServiceAccount.LocalSystem;            Installers.Add(processinstaller);        }

 

然後,把剛才安裝的服務卸載掉,執行installUtil /u xxx.exe。

接著再次產生項目,並進行安裝,然後,在服務管理員中就看到兩個服務了。

 

這麼個簡單的實驗,再次驗證了:一個服務安裝器只能用於安裝一個服務,一個服務必須進行安裝後才能啟動

 

範例程式碼

 

編寫Windows服務疑問2:探索服務與安裝器的關係

聯繫我們

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