简体   繁体   English

如何安装在.NET 3.5中开发的Windows服务?

[英]How to install a Windows service developed in .NET 3.5?

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. 通常,InstallUtil.exe将用于安装Windows服务,但.NET 3.5中不提供InstallUtil.exe实用程序。 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. 当我尝试使用.NET 2.0安装该服务时,该服务将显示在服务列表中,但在启动服务时,Windows服务错误1053即将到来。 How we can avoid this problem and successfully install the service? 我们如何避免这个问题并成功安装服务?

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: 因此,在您的服务项目中,您希望:

  1. In the solution explorer double click your services .cs file. 在解决方案资源管理器中,双击您的服务.cs文件。 It should bring up a screen that is all gray and talks about dragging stuff from the toolbox. 它应该显示一个全灰色的屏幕,并讨论从工具箱中拖动内容。
  2. Then right click on the gray area and select add installer. 然后右键单击灰色区域并选择添加安装程序。 This will add an installer project file to your project. 这会将安装程序项目文件添加到项目中。
  3. Then you will have 2 components on the design view of the ProjectInstaller.cs (serviceProcessInstaller1 and serviceInstaller1). 然后,您将在ProjectInstaller.cs(serviceProcessInstaller1和serviceInstaller1)的设计视图中拥有2个组件。 You should then setup the properties as you need. 然后,您应该根据需要设置属性。

Now you need to make a setup project. 现在你需要进行一个安装项目。 The best thing to do is use the setup wizard. 最好的办法是使用设置向导。

  1. Right click on your solution and add a new project: Add > New Project > Setup and Deployment Projects > Setup Wizard 右键单击解决方案并添加新项目:添加>新建项目>设置和部署项目>设置向导
  2. On the second step select "Create a Setup for a Windows Application." 在第二步选择“为Windows应用程序创建安装程序”。
  3. On the 3rd step, select "Primary output from..." 在第3步,选择“主要输出...”
  4. Click through to Finish. 单击“完成”。

Now you need to edit your installer to make sure the correct output is included. 现在,您需要编辑安装程序以确保包含正确的输出。

  1. Right click on the setup project in your Solution Explorer. 右键单击解决方案资源管理器中的安装项目。
  2. Select View > Editor > Custom Actions. 选择视图>编辑器>自定义操作。
  3. Right-click on the Install action in the Custom Actions tree and select 'Add Custom Action...' 右键单击“自定义操作”树中的“安装”操作,然后选择“添加自定义操作...”
  4. In the "Select Item in Project" dialog, select Application Folder and click OK. 在“在项目中选择项目”对话框中,选择“应用程序文件夹”并单击“确定”
  5. Click OK to select "Primary output from..." option. 单击“确定”以选择“主输出...”选项。 A new node should be created. 应该创建一个新节点。
  6. 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. 现在只需构建您的安装程序,它将生成一个MSI和一个setup.exe。 Choose whichever you want to use to deploy your service. 选择要用于部署服务的任何一种。

There's a Microsoft KB on this for .Net 2.0 and VS2005. 对于.Net 2.0和VS2005,有一个Microsoft KB。 The procedure is exactly the same in .Net 3.5 and VS2008. 该程序在.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.) (有时KB不像你在其他地方找到的教程那么友好。)

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

If you've been using InstallUtil.exe to install your Windows service, then that means you've added a ProjectInstaller component to your service. 如果您一直使用InstallUtil.exe来安装Windows服务,那么这意味着您已将ProjectInstaller组件添加到您的服务中。 All the InstallUtil.exe does is use reflection to find the installer component embedded in your service and execute some methods on it. 所有InstallUtil.exe都使用反射来查找服务中嵌入的安装程序组件并在其上执行一些方法。 Due to this, you can modify your Windows service to install and uninstall itself, ie, you no longer have to depend on InstallUtil.exe being available on the target machine . 因此,您可以修改Windows服务以自行安装和卸载,即您不再需要依赖目标计算机上可用的InstallUtil.exe 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 . 这个想法最初属于Marc Gravell和这篇文章

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. 通常是对启动Windows服务的问题的一般响应。 What you should do is check the event log and likely you will find the real error that is preventing the service from starting. 你应该做的是检查事件日志,你可能会发现阻止服务启动的真正错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM