2010-11-30 46 views
2

有很多的例子來說明如何在一行中安裝Windows服務:的.Net Windows服務:從引用的程序集安裝

ManagedInstallClass.InstallHelper(
     new[] { Assembly.GetExecutingAssembly().Location }); 

直到服務類中的exe模塊聲明工作正常。 但是,如果服務類在引用程序集(未在可執行文件中聲明,但在鏈接的dll中),相同的代碼不適用於我。

在這種情況下,服務也被註冊,但無法啓動,因爲它是註冊與DLL路徑並指向DLL(「服務不是一個WIN32可執行文件」消息出現在事件日誌中,當我嘗試啓動)

如果我將GetExecutingAssembly().Location更改爲可執行路徑,則不會找到安裝程序,並且根本沒有註冊服務。

是否可以將服務類放入引用的程序集中,並且仍然能夠以最小的努力註冊服務?

預先感謝您!

回答

3

這裏是一些C#代碼,允許用戶安裝/卸載服務「手動」(無需申報的runInstaller定製屬性):

static void InstallService(string path, string name, string displayName, string description) 
{ 
    ServiceInstaller si = new ServiceInstaller(); 
    ServiceProcessInstaller spi = new ServiceProcessInstaller(); 
    si.Parent = spi; 
    si.DisplayName = displayName; 
    si.Description = description; 
    si.ServiceName = name; 
    si.StartType = ServiceStartMode.Manual; 

    // update this if you want a different log 
    si.Context = new InstallContext("install.log", null); 
    si.Context.Parameters["assemblypath"] = path; 

    IDictionary stateSaver = new Hashtable(); 
    si.Install(stateSaver); 
} 

static void UninstallService(string name) 
{ 
    ServiceInstaller si = new ServiceInstaller(); 
    ServiceProcessInstaller spi = new ServiceProcessInstaller(); 
    si.Parent = spi; 
    si.ServiceName = name; 

    // update this if you want a different log 
    si.Context = new InstallContext("uninstall.log", null); 
    si.Uninstall(null); 
}