C# 如何安装在 .NET 3.5 中开发的 Windows 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1541630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install a Windows service developed in .NET 3.5?
提问by srv
I have developed a Windows service using Visual Studio 2008. I want to install that service in a machine where Visual Studio is not installed, but .NET 3.5 is installed.
我已经使用 Visual Studio 2008 开发了一个 Windows 服务。我想在没有安装 Visual Studio 但安装了 .NET 3.5 的机器上安装该服务。
Generally InstallUtil.exe shall be used for installing a Windows Service, but the InstallUtil.exe utility is not available in .NET 3.5. When I tried installing that service using .NET 2.0, the service is getting displayed in the list of services but when starting the service Windows Service error 1053 is coming. How we can avoid this problem and successfully install the service?
通常 InstallUtil.exe 应用于安装 Windows 服务,但 InstallUtil.exe 实用程序在 .NET 3.5 中不可用。当我尝试使用 .NET 2.0 安装该服务时,该服务将显示在服务列表中,但在启动该服务时,Windows 服务将出现错误 1053。我们如何才能避免这个问题并成功安装服务?
采纳答案by David
There's a Microsoft KB on this for .Net 2.0 and VS2005. The procedure is exactly the same in .Net 3.5 and VS2008.
有一个关于 .Net 2.0 和 VS2005 的 Microsoft KB。该过程在 .Net 3.5 和 VS2008 中完全相同。
http://support.microsoft.com/kb/317421
http://support.microsoft.com/kb/317421
And here's a nicer article with pictures to make it clearer. (Sometimes the KB's aren't as friendly as tutorials you can find elsewhere.)
这是一篇带有图片的更好的文章,以使其更清晰。(有时知识库不像您可以在其他地方找到的教程那样友好。)
http://aspalliance.com/1316_Working_with_Windows_Service_Using_Visual_Studio_2005.3
http://aspalliance.com/1316_Working_with_Windows_Service_Using_Visual_Studio_2005.3
回答by Matt Davis
If you've been using InstallUtil.exe to install your Windows service, then that means you've added a ProjectInstaller component to your service. All the InstallUtil.exe does is use reflection to find the installer component embedded in your service and execute some methods on it. Due to this, you can modify your Windows service to install and uninstall itself, i.e., you no longer have to depend on InstallUtil.exe being available on the target machine. I've been using this successfully for several months now. Just follow the step-by-step I provided here. The idea originally belongs to Marc Gravell and this post.
如果您一直在使用 InstallUtil.exe 来安装 Windows 服务,那么这意味着您已将 ProjectInstaller 组件添加到您的服务中。InstallUtil.exe 所做的只是使用反射来查找嵌入在您的服务中的安装程序组件并在其上执行一些方法。因此,您可以修改您的 Windows 服务来安装和卸载它自己,即您不再需要依赖于在目标机器上可用的 InstallUtil.exe。我已经成功地使用了几个月了。只需按照我在此处提供的分步操作即可。这个想法最初属于 Marc Gravell 和这篇文章。
回答by Kelsey
It's actually really simple as I just did it a couple of days ago for something I made.
这实际上非常简单,因为我几天前刚刚为我制作的东西做了它。
So in your service project you want to:
因此,在您的服务项目中,您希望:
- In the solution explorer double click your services .cs file. It should bring up a screen that is all gray and talks about dragging stuff from the toolbox.
- Then right click on the gray area and select add installer. This will add an installer project file to your project.
- Then you will have 2 components on the design view of the ProjectInstaller.cs (serviceProcessInstaller1 and serviceInstaller1). You should then setup the properties as you need.
- 在解决方案资源管理器中双击您的服务 .cs 文件。它应该显示一个全灰色的屏幕,并讨论从工具箱中拖动东西。
- 然后右键单击灰色区域并选择添加安装程序。这会将安装程序项目文件添加到您的项目中。
- 然后您将在 ProjectInstaller.cs 的设计视图上拥有 2 个组件(serviceProcessInstaller1 和 serviceInstaller1)。然后,您应该根据需要设置属性。
Now you need to make a setup project. The best thing to do is use the setup wizard.
现在您需要制作一个安装项目。最好的办法是使用设置向导。
- Right click on your solution and add a new project: Add > New Project > Setup and Deployment Projects > Setup Wizard
- On the second step select "Create a Setup for a Windows Application."
- On the 3rd step, select "Primary output from..."
- Click through to Finish.
- 右键单击您的解决方案并添加一个新项目:添加 > 新建项目 > 安装和部署项目 > 安装向导
- 在第二步中,选择“为 Windows 应用程序创建安装程序”。
- 第三步,选择“Primary output from...”
- 单击以完成。
Now you need to edit your installer to make sure the correct output is included.
现在您需要编辑安装程序以确保包含正确的输出。
- Right click on the setup project in your Solution Explorer.
- Select View > Editor > Custom Actions.
- Right-click on the Install action in the Custom Actions tree and select 'Add Custom Action...'
- In the "Select Item in Project" dialog, select Application Folder and click OK.
- Click OK to select "Primary output from..." option. A new node should be created.
- Repeat steps 4 - 5 for commit, rollback and uninstall actions.
- 右键单击解决方案资源管理器中的安装项目。
- 选择“视图”>“编辑器”>“自定义操作”。
- 右键单击自定义操作树中的安装操作,然后选择“添加自定义操作...”
- 在“选择项目中的项目”对话框中,选择应用程序文件夹并单击确定。
- 单击“确定”以选择“来自...的主要输出”选项。应该创建一个新节点。
- 对提交、回滚和卸载操作重复步骤 4 - 5。
Now just build your installer and it will produce an MSI and a setup.exe. Choose whichever you want to use to deploy your service.
现在只需构建您的安装程序,它将生成一个 MSI 和一个 setup.exe。选择要用于部署服务的任何一个。
回答by atconway
The message:
消息:
"Error 1053: The service did not respond to the start or control request in a timely fashion."
“错误 1053:服务没有及时响应启动或控制请求。”
is typically a generic response to an issue starting the Windows Service. What you should do is check the event log and likely you will find the realerror that is preventing the service from starting.
通常是对启动 Windows 服务的问题的一般响应。您应该做的是检查事件日志,您可能会发现阻止服务启动的真正错误。