2010-11-30 91 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); 
}