在 C# .NET 中创建 Windows 服务的最佳指南?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/269603/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:54:58  来源:igfitidea点击:

Best guide for creating Windows Services in C# .NET?

c#.netwindows-services

提问by Presentation Avenger

I'm looking to convert a small .NET console application into a Windows Service. I'd like to build two versions, one using .NET 2.0 and another with .NET 3.5 .

我希望将小型 .NET 控制台应用程序转换为 Windows 服务。我想构建两个版本,一个使用 .NET 2.0 ,另一个使用 .NET 3.5 。

Are there radically different approaches that need to be taken, or will the 2.0 version be roughly equivalent to the 3.5 version? Where's a good source of information (i.e. a web-based guide) that can walk me through the steps of setting up the service?

是否需要采取完全不同的方法,或者 2.0 版本是否与 3.5 版本大致相同?哪里有可以引导我完成设置服务步骤的良好信息来源(即基于 Web 的指南)?

Thanks! P.A.

谢谢!公共广播

回答by Scott Dorman

The thing to remember is that .NET 3.5 is a set of additional libraries on top of .NET 2.0, so, unless you are planning on taking advantage of any additional features provided by .NET 3.5 (or .NET 3.0) like LINQ or WCF the code would be identical.

需要记住的是 .NET 3.5 是 .NET 2.0 之上的一组附加库,因此,除非您计划利用 .NET 3.5(或 .NET 3.0)提供的任何附加功能,如 LINQ 或 WCF代码将是相同的。

Try looking at the documentation for ServiceBase, which is the base class that you will need to inherit, to get things started.

尝试查看ServiceBase的文档,它是您需要继承的基类,以开始工作。

回答by HitLikeAHammer

Actually, .NET 3.5 doeschange the C# code a little. For example, you can use the var keyword, and you can use the hidden private variables for properties. It is still based on CLR 2.0.

实际上,.NET 3.5确实稍微改变了 C# 代码。例如,您可以使用 var 关键字,并且您可以将隐藏的私有变量用于属性。它仍然基于 CLR 2.0。

There is a pretty good article at msdnthat talks about windows services and walks you through building one.

msdn上有一篇很好的文章它讨论了 Windows 服务并指导您构建一个服务。

回答by Timothy Carter

ThisI believe is a decent walk through with screenshots and code samples. I think it may have been what I used for the first windows service I wrote. I think it was written back in .NET 1.1, but should still help walk you through the process. As far as the differences between 2.0 and 3.5, I would say there can/will be as much difference as you want there to be. I don't believe you will be required to change anything, but as other posters have mentioned and as you can find all over SO, there are a lot of new features that can be very beneficial that came with .NET 3.5.

我相信是一个不错的截图和代码示例。我想这可能是我写的第一个 Windows 服务所使用的。我认为它是在 .NET 1.1 中写回来的,但仍然可以帮助您完成整个过程。至于 2.0 和 3.5 之间的差异,我想说可以/将会有你想要的差异。我不相信你会被要求改变任何东西,但正如其他海报所提到的那样,你可以在 SO 上找到很多新功能,这些新功能非常有益于 .NET 3.5。

回答by crftr

To supplement Rick's answer, I'd suggest the MSDN Walkthrough

为了补充 Rick 的回答,我建议使用MSDN 演练

It is incredibly verbose and touches on event logging as well as an installer.

它非常冗长,涉及事件日志和安装程序。

回答by Scott

There is at least one place where service code will need some attention to make it work in .net2/vs2005 vs. .net35/vs2008.

至少有一个地方需要注意服务代码才能使其在 .net2/vs2005 和 .net35/vs2008 中工作。

This is especially true if you write your service and installer using in c# 2.0 and compile and deploy using .net35/vs2008, the link below might save you a bit of time:

如果您使用 c# 2.0 编写服务和安装程序并使用 .net35/vs2008 编译和部署,则尤其如此,以下链接可能会为您节省一些时间:

Custom Actions Changed

自定义操作已更改

回答by Praveen

Here is guideto learn about windows service.

这里是学习指南windows service