【问题标题】:Deploy a windows service without Installshield在没有 Installshield 的情况下部署 Windows 服务
【发布时间】:2014-10-05 09:05:21
【问题描述】:
是否可以在不通过 Installshield 的情况下部署 Windows 服务?我有一个非常基本的服务,它只是探测数据库,并希望将其部署在服务器上。
我尝试使用 Installsheild LE,但安装时出现错误 1001,这很难解决,无论如何 Installshield 在这种情况下感觉有点过头了...有没有办法可以直接通过命令行或其他方法安装服务?
【问题讨论】:
标签:
visual-studio-2012
windows-services
installshield
【解决方案1】:
是的。
但是,有一些选项,从最好到最差:
还有其他用于编写安装程序的工具。例如。 install Shield 的完整版或WiX(由 MS 创建,用于 Visual Studio 安装程序)。
您可以使用 installUtil 并在您的程序集中包含派生自 ServiceInstaller 的类型。 (见How to: Install and Uninstall Service。)
您可以手动编辑注册表。
#3 非常容易搞砸,不会帮助您处理应该包括的事件日志记录和性能计数器。1 #2 是开发中最好的,并且会设置事件日志和性能计数器(请记住,您需要升级才能安装东西,并在作为服务运行时将调试器附加到服务)。
在测试(暂存)和生产环境中最好使用真正的安装程序(#1)。
1当你被问到为什么它不起作用时,你会希望能够弄清楚发生了什么(或没有发生)。
【解决方案2】:
是的,如果您的服务有 Installer 类,那么您可以使用 installutil.exe 从命令行安装它
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
[RunInstaller(true)]
public class ServiceInstaller : Installer
{
public ServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.User;
serviceProcessInstaller.Username = "";
serviceProcessInstaller.Password = "";
//# Service Information
serviceInstaller.DisplayName = "Service name"
serviceInstaller.Description = "Service description"
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "Service Name";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}