简体   繁体   English

使用.net启动Windows服务开发

[英]starting windows service development using .net

I have started working with the C# from couple of months and now i am starting with the windows service. 我从几个月开始使用C#,现在我开始使用Windows服务。 MSDN is good as always to start with but i would like to know if there is book, blog ... etc (dedicated) for this which could be a nice starting point ? MSDN一如既往的好,但我想知道是否有书,博客......等(专用)这可能是一个很好的起点?

It the same as a console app, just without a console (and a little extra setup). 它与控制台应用程序相同,只是没有控制台(以及一些额外的设置)。

I have written many console apps that can run as a service too, you can achieve this in less than 5 lines of code. 我已经编写了许多可以作为服务运行的控制台应用程序,您可以在不到5行代码中实现这一点。

Your best bet is just to use the default VS template for a service, and see how little difference there really is. 您最好的选择就是使用默认的VS模板进行服务,看看实际上差异不大。

There are some things to watch out for however. 但是有一些事情需要注意。

  • Your service needs to do something, else it will just exit after starting. 您的服务需要做一些事情,否则它将在启动后退出。 I normally just start off another thread with the workload. 我通常只是从工作负载的另一个线程开始。
  • You need to be able to stop your service. 您需要能够停止服务。 Using a thread as above provides an ugly but foolproof way. 使用上面的线程提供了一种丑陋但万无一失的方式。
  • Services can't (or should not) interact with the desktop. 服务不能(或不应该)与桌面交互。

What is the reason for choosing windows services as you execution environment? 在执行环境中选择Windows服务的原因是什么? Generally, these are avoided if possible (particularly for people new to .NET). 通常,如果可能,可以避免这些(特别是对于.NET新手)。 They can be sort of cranky and are rather opaque. 它们可能有点胡思乱想,而且相当不透明。 By all means, if your requirements neccesatate a windows service, go for it, but if not, I'd start elsewhere. 无论如何,如果你的要求需要一个Windows服务,那就去吧,但如果没有,我会从其他地方开始。

如果要使用C#和Visual Studio 2008创建Windows服务,可以按照此处的分步说明进行操作。

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

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