C# 如何在应用程序安装过程中安装 Windows 服务 [英] C# How to install Windows Service as part of application installation

查看:41
本文介绍了C# 如何在应用程序安装过程中安装 Windows 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个安装程序,它从选择的子项目中获取主要输出,构成我的应用程序的子项目之一是 Windows 服务.此服务需要作为主应用程序的一部分进行安装.

I have created an installer that takes the primary output from a selection of sub-projects, one of the sub-projects that make up my application is a Windows Service. This service needs to be installed as part of the main application.

到目前为止,我已经尝试将安装程序添加到服务项目,单独发布并安装生成的 Setup.exe 作为项目安装程序的自定义操作,但使用此方法没有任何成功.我会从命令行安装这个东西,但是我需要在 Visual Studio CMD 工具中打包才能访问 installutil 命令.

So far I have tried adding an installer to the Service project, publishing this separately and installing the resultant Setup.exe as a custom action for the project installer but have not had any success with this method. I would install the thing from the command line, however I'd need to then package in the Visual Studio CMD Tool to get access to the installutil command.

我的问题是,我可以从Visual Studio 安装程序 - 安装项目"安装 Windows 服务作为较大项目的一部分吗?如果可以,如何安装?到目前为止,我还没有了解这个过程.

My question is, can I install a Windows Service from a "Visual Studio Installer - Setup Project" as part of a larger project and if so how? As the process has eluded me thus-far.

我想我可以尝试让自定义操作触发一个脚本来执行 ServiceInstaller 作为流程的一部分,所以我会尝试这样做.

I suppose I could try having the custom action trigger a script that executes the ServiceInstaller as part of the process, so I'll be trying that.

提前致谢

推荐答案

由于 VS2013 不包含旧的 (VS2010) Visual Studio 安装程序项目,我有以下建议:

Since VS2013 does not contain the old (VS2010) Visual Studio installer project, I have these suggestions:

使用 WiX 安装程序:https://wix.codeplex.com

甚至还有一个服务安装程序模板(未测试):

There is even a service installer template (not tested):

https://visualstudiogallery.msdn.microsoft.com/7f35c8ce-1763-4340-b720-ab2d359009c5

使用 VS 2013 安装扩展

有很多抱怨 MS 放弃了旧的安装项目,所以他们把它作为扩展带回来:

There were many complaints that MS abandoned the old setup project, so they brought it back as an extension:

http://blogs.msdn.com/b/visualstudio/archive/2014/04/17/visual-studio-installer-projects-extension.aspx

我尚未对此进行测试,但据我了解,它将带回内部 VS 安装项目类型的旧功能.

I have not yet tested this, but as I understand it, it will bring back the old capabilities of the internal VS setup project type.

我已经使用 VS2010 安装项目编写了一些 Windows 服务,甚至编写了如何执行此操作的分步说明.有些步骤不是一目了然,因此我自己将其用作服务项目的指南.

I have written some Windows services using VS2010 setup project and even wrote a step-by-step instruction how to do this. Some steps are not self-explanatory, so I use this myself as a guide for service projects.

我使用 WiX 进行了一些初步测试,这似乎是一个不错的解决方案,但我还没有将其用于服务安装.

I did some first tests with WiX, which seems a good solution, however I had not yet used that for service installation.

关于此主题的另一个链接:

Another link regarding this topic:

https://superuser.com/questions/727643/installing-a-windows-service-without-visual-studios-installutil-exe

我打算很快检查我关于使用 VS2013 安装服务的分步说明,并可能将其与 WiX 进行比较,如果您有兴趣,请告诉我,我可以将其公开.

I plan to check my step-by-step instructions regarding service installation with VS2013 soon and probably compare this to WiX, if you are interested, please tell me, I can make this publicly available.

这是我创建 Windows 服务的分步说明.(您也可以在 http://www.rsprog.de/samplewindowsservice 上找到此内容)

Here are my step-by-step instructions for creating a Windows service. (you can find this also on http://www.rsprog.de/samplewindowsservice)

  • 文件 => 新建 => 项目...

  • File => New => Project...

Visual C# => Windows 桌面 => Windows 服务

Visual C# => Windows Desktop => Windows Service

名称:SampleService

Name: SampleService

选中创建解决方案目录"

"Create directory for solution" checked

在解决方案资源管理器中,选择 Service1.cs,右键单击 => 重命名

In Solution Explorer, select Service1.cs, right-click => Rename

将文件重命名为 SampleService.cs

Rename file to SampleService.cs

为...您是否还想执行重命名..."选择是

Select Yes for "... Would you also like to perform a rename ..."

SampleService.cs 现在应该显示在设计视图中

SampleService.cs should be shown in Design View now

  • 在设计器视图的背景中点击

  • Click in the background of the designer view

右击 => 添加安装程序

right-click => Add Installer

(添加了 serviceProcessInstaller1 和 serviceInstaller1)

(serviceProcessInstaller1 and serviceInstaller1 have been added)

  • 在设计视图中打开 SampleService.cs

  • open SampleService.cs in design view

在属性中,将 ServiceName 设置为 SampleService

in Properties, set ServiceName to SampleService

在设计视图中打开 ProjectInstaller.cs

open ProjectInstaller.cs in Design View

点击 serviceInstaller1

Click serviceInstaller1

在属性"窗口中,将 ServiceName 属性设置为 SampleService.

In the Properties window, set the ServiceName property to SampleService.

将 DisplayName 属性设置为 Sample Service 或其他内容(稍后将在服务控制台中显示为服务名称)

Set the DisplayName property to Sample Service or something else (This will be shown later in services console as service name)

在这里您还可以选择将 StartType 从手动设置为自动

Here you can also optionlly set StartType from Manual to Automatic

点击 serviceProcessInstaller1

click serviceProcessInstaller1

如果设置为User,设置时会弹出一个允许设置用户/密码的弹窗

If you set it as User, a popup during setup will allow user / password to be set

  • 在解决方案处右键单击 => 添加 => 新建项目...

  • At Solution right-click => Add => New Project...

其他项目类型 => Visual Studio 安装程序 => 安装项目

Other Project Types => Visual Studio Installer => Setup Project

名称:SampleServiceSetup

Name: SampleServiceSetup

选择 SampleServiceSetup 项目,右键单击 => 添加 => 项目输出...

Select SampleServiceSetup project, right-click => Add => Project Output...

因为项目SampleService => Primary output"应该已经被选中,因为应该已经选择了配置(活动)".

As Project "SampleService => Primary output" should be already selected, as Configuration "(Active)" should be already selected.

按确定

在属性视图中选择 SampleServiceSetup 项目:

Select SampleServiceSetup project, in properties view:

默认情况下,安装项目的目标平台是 x86.您可以在此处将其更改为 x64 (TargetPlatform)

By default, the target platform of the setup project is x86. You can change it here to x64 (TargetPlatform)

  • 在解决方案资源管理器中右键单击安装项目 => 查看 => 自定义操作

  • Right-click the setup project in Solution Explorer => View => Custom Actions

在自定义操作视图中,右键单击自定义操作节点 => 添加自定义操作...

In the Custom Actions view, right-click the Custom Actions node => Add Custom Action...

双击列表中的应用程序文件夹将其打开,从 SampleService (Active) 中选择 Primary Output,然后单击 OK.(SampleService 的主要输出(活动)"已添加到四个节点)

Double-click the Application Folder in the list to open it, select Primary Output from SampleService (Active), and click OK. ("Primary output from SampleService (Active)" was added to the four nodes)

选择 SampleServiceSetup 项目,右键单击 => 查看 => 用户界面

Select SampleServiceSetup project, right-click => View => User Interface

开始 => 安装文件夹 => 属性 => InstallAllUsersVisible:更改为 False

Start => Installation Folder => Properties => InstallAllUsersVisible: change to False

构建解决方案

这篇关于C# 如何在应用程序安装过程中安装 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆