如何将VS解决方案作为Windows服务 [英] How to make VS solution as Windows Service

查看:85
本文介绍了如何将VS解决方案作为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VS解决方案中有2个项目。其中之一是Web应用程序。我使用nancy来构建Web应用程序。

这里我想将我的项目安装为windows服务。我使用sc命令在Windows服务列表中将项目安装为服务。

但我启动它显示异常的服务,如服务没有及时响应启动或控制请求 。如何解决这个异常。



这里我的用例是:

当我启动一个服务时,它会在内部启动2个进程并自我主机网页。

当我停止服务时它将停止进程将服务启动选项更改为手动。

应用程序运行直到我停止服务。

解决方案

首先,您不能只安装任何项目(您应该将构建项目的结果,一些程序集)作为Windows服务。不,您需要开发Windows服务。你可以从这里开始:

https:/ /msdn.microsoft.com/en-us/library/y817hyb6%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx [ ^ ]。



您没有解释为什么您会通过服务启动两个进程以及这些进程应该做什么,但通常情况下,这不会似乎是一个充分的解决方案。服务进程由服务控制器托管。当用户注销时,它将继续执行。它不适用于内部创建的流程。我认为做这些事情没有任何意义。在您的服务过程中,您可以创建任意数量的线程。他们可以扮演你为这些内部流程设计的任何角色,这个解决方案将足以满足服务应用程序的属性。



无论如何,你可以开始一些过程调用 System.Diagnostics.Process.Start

https://msdn.microsoft.com/en-us/library/system.diagnostics.process.start%28v=vs.110% 29.aspx [ ^ ]。

但我不会推荐它,因为正如我上面解释的那样,这个过程将托管在用户的会话中,在开发服务时是错误的。



现在,还不清楚为什么在停止进程时更改启动选项。显然,如果服务已停止,则其进程不存在(如果您不知道),则必须在进程停止之前执行某些操作。很简单,您可以通过在实现服务的派生类中重写虚拟方法 System.ServiceProcess.ServiceBase.OnStop 来处理它: https://msdn.microsoft.com/en-us/library/system.serviceprocess。 servicebase.onstop%28v = vs.110%29.aspx [ ^ ]。



现在,你想自我托管一个网页。这很可能,您在Windows服务中创建的一个或多个线程可以做到这一点,扮演一些HTTP服务的角色。此外,我想它不一定是一个完全成熟的HTTP服务器,它具有所有插件支持(这是通常支持不同服务器端技术的方式),保护和其他花哨。它只需要支持HTTP协议并提供一些专门的上下文(或不那么专业)。这不是一个大问题。你可以从这里开始:

https://msdn.microsoft.com/en-us/library/system.web.http.httpserver%28v=vs.118%29.aspx [ ^ ]。< br $> b $ b

如果我错过了什么,请随时提出一些后续问题。



- SA

I have a 2 Projects in VS solution. one of that is web application. I use nancy to build web application.
Here I want to install my project as windows service. I use sc command to install the project as service in Windows service list.
But I start the service it shows exception like this "The service did not respond to the start or control request in the timely fashion". How to resolve this exception.

Here My usecase is:
when I start a service it will start the 2 processes internally and selfhost the web page.
when I stop the service it will stop the processes change the service startup option into manual.
The application is running until I stop the service.

解决方案

First of all, you cannot just install any arbitrary "project" (you should rather talk about the result of building a project, some assembly) as Windows Service. No, you need to develop a Windows Service. You can start here:
https://msdn.microsoft.com/en-us/library/y817hyb6%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx[^].

You did not explain why would you start two processes by a service and what those processes should do, but normally, this doesn't seem to be an adequate solution. The service process is hosted by a service controller. When a user logs off, it continues its execution. It won't work with the processes created "internally". I see no sense in doing such things. In your service process, you can create any number of threads. They can play any role you devised for those "internal processes" and this solution will be adequate to the properties of the service application.

Anyway, you can start some process calling System.Diagnostics.Process.Start:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.start%28v=vs.110%29.aspx[^].
But I won't recommend it, as the process, as I explained above, will be hosted in the user's session, which is wrong thing when you are developing a service.

Now, it is also unclear why would you change the startup options when you stop the process. Apparently, if the service is stopped, its process does not exist (in case you did not know that), so you have to perform some action just before the process is stopped. Quite simply, you can handle it by overriding the virtual method System.ServiceProcess.ServiceBase.OnStop in your derived class implementing the service: https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstop%28v=vs.110%29.aspx[^].

Now, you wanted to self-host a Web page. This is quite possible, one of more of the threads you create in your Windows Service can do that, play the role of some HTTP service. Also, I guess it does not have to be a fully-fledged HTTP server with all its plug-in support (this is how different server-side technologies are normally supported), protection and other bells and whistles. It just have to support HTTP protocol and serve up some specialized context (or not so specialized). This is not a big problem. You can start here:
https://msdn.microsoft.com/en-us/library/system.web.http.httpserver%28v=vs.118%29.aspx[^].

If I missed something, feel free to ask some follow-up questions.

—SA


这篇关于如何将VS解决方案作为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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