我如何使用Visual Studio创建的Windows服务创建安装程序?
我如何使用Visual Studio创建的Windows服务创建安装程序?
右键单击您的解决方案并添加新项目:添加 > 新建项目 > 安装和部署项目 > 安装向导
a.不同版本的Visual Studio可能略有不同。 b.Visual Studio 2010位于:安装模板 > 其他项目类型 > 安装和部署 > Visual Studio Installer
在第二步中选择“为Windows应用程序创建安装程序。”
在第三步中,选择“来自...的主要输出”
一路点击完成。
您可以通过右键单击解决方案中的安装程序项目并选择属性来编辑安装程序的输出名称。将“输出文件名:”更改为您想要的任何内容。同时选择安装程序项目并查看属性窗口,您可以编辑产品名称
、标题
、制造商
等…
接下来构建您的安装程序,它将生成一个MSI和一个setup.exe。选择您想要使用的任何一个来部署您的服务。
我按照Kelsey的第一组步骤将安装程序类添加到我的服务项目中,但是我没有创建MSI或setup.exe安装程序,而是使服务自行安装/卸载。这是我其中一个服务的示例代码,您可以将其用作起点。
public static int Main(string[] args)
{
if (System.Environment.UserInteractive)
{
// we only care about the first two characters
string arg = args[0].ToLowerInvariant().Substring(0, 2);
switch (arg)
{
case "/i": // install
return InstallService();
case "/u": // uninstall
return UninstallService();
default: // unknown option
Console.WriteLine("Argument not recognized: {0}", args[0]);
Console.WriteLine(string.Empty);
DisplayUsage();
return 1;
}
}
else
{
// run as a standard service as we weren't started by a user
ServiceBase.Run(new CSMessageQueueService());
}
return 0;
}
private static int InstallService()
{
var service = new MyService();
try
{
// perform specific install steps for our queue service.
service.InstallService();
// install the service with the Windows Service Control Manager (SCM)
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
}
catch (Exception ex)
{
if (ex.InnerException != null && ex.InnerException.GetType() == typeof(Win32Exception))
{
Win32Exception wex = (Win32Exception)ex.InnerException;
Console.WriteLine("Error(0x{0:X}): Service already installed!", wex.ErrorCode);
return wex.ErrorCode;
}
else
{
Console.WriteLine(ex.ToString());
return -1;
}
}
return 0;
}
private static int UninstallService()
{
var service = new MyQueueService();
try
{
// perform specific uninstall steps for our queue service
service.UninstallService();
// uninstall the service from the Windows Service Control Manager (SCM)
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
}
catch (Exception ex)
{
if (ex.InnerException.GetType() == typeof(Win32Exception))
{
Win32Exception wex = (Win32Exception)ex.InnerException;
Console.WriteLine("Error(0x{0:X}): Service not installed!", wex.ErrorCode);
return wex.ErrorCode;
}
else
{
Console.WriteLine(ex.ToString());
return -1;
}
}
return 0;
}
无论是 Kelsey 还是 Brendan 的解决方案,在 Visual Studio 2015 Community 中都不适用于我。
以下是我创建带有安装程序的服务的简要步骤:
->
新建->
项目双击 serviceInstaller1。Visual Studio 创建了 serviceInstaller1_AfterInstall
事件。编写代码:
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
using (System.ServiceProcess.ServiceController sc = new
System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName))
{
sc.Start();
}
}
构建解决方案。右键单击项目,选择“在文件资源管理器中打开文件夹”。前往bin\Debug。
使用以下脚本创建install.bat:
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
cd /d %~dp0
%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /i "WindowsService1.exe"
pause
/i
更改为/u
)InstallUtil类(ServiceInstaller)被Windows Installer社区认为是一种反模式。它是一个脆弱的、进程外的、重复造轮子的工具,忽略了Windows Installer已经内置了对服务的支持。
Visual Studio部署项目(在下一个版本的Visual Studio中也不受高度评价并且已被弃用)没有原生支持服务。但是它们可以使用合并模块。因此,我建议您查看这篇博客文章,了解如何使用Windows Installer XML创建一个可以表达服务的合并模块,并在您的VDPROJ解决方案中使用该合并模块。