C# Windows 服务与 Windows 应用程序 - 最佳实践

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

Windows Service vs Windows Application - Best Practice

c#windowswinformswindows-services

提问by Mugunth

When should I go for a Windows Service and when should I go for a "Background Application" that runs in the notification area?

我什么时候应该选择 Windows 服务,什么时候应该选择在通知区域运行的“后台应用程序”?

If I'm not wrong, my design decision would be, any app that needs to be running before the user logins to the computer should be a service. For everything else use a background app. Is my decision right?

如果我没记错的话,我的设计决定是,任何需要在用户登录计算机之前运行的应用程序都应该是一项服务。对于其他一切,请使用后台应用程序。我的决定对吗?

Moreover, if I need "admin privileges" for my background app, I would escalate using a manifest. Are there any other specific advantage of running as a service?

此外,如果我的后台应用程序需要“管理员权限”,我会使用清单进行升级。作为服务运行还有其他特定优势吗?

采纳答案by blowdart

My general rules of thumb are as follows

我的一般经验法则如下

  • If it needs to always run, it's a service.
  • If it needs to be run under a particular user account, Network Service, Local System, generally it's a service (or a COM+ application)
  • If the user needs some control over it, it's generally a notification area application.
  • If it needs to notify the user of something, it's a notification area application
  • 如果它需要始终运行,则它是一项服务。
  • 如果它需要在特定用户帐户、网络服务、本地系统下运行,通常它是一个服务(或 COM+ 应用程序)
  • 如果用户需要对其进行一些控制,它通常是一个通知区域应用程序。
  • 如果它需要通知用户某事,它是一个通知区域应用程序

The fun comes when you have the need to run something as a system account, but also interact with it. IIS is a good example of this, it's a service, but the administration is an application - it needs to be running at the startup, it needs access to particular things a user can't normal access (c:\inetpub), but the user needs to be able to start, stop and configure it.

当您需要以系统帐户的身份运行某些内容,但也需要与之交互时,乐趣就来了。IIS 就是一个很好的例子,它是一个服务,但管理是一个应用程序——它需要在启动时运行,它需要访问用户无法正常访问的特定内容 (c:\inetpub),但是用户需要能够启动、停止和配置它。

回答by Sev

I believe your decision is almost right, however, I would add one more condition. Take for example the mysqld service (you have a choice in this case, but most people run it as a service). It's ran as a service because you want to access the service anytime through potentially multiple applications. It's important that it is responsive to all applications calling upon it, and itself, is not much of anything, other than waiting to serve other applications.

我相信您的决定几乎是正确的,但是,我还要添加一个条件。以 mysqld 服务为例(在这种情况下您可以选择,但大多数人将其作为服务运行)。它作为服务运行,因为您希望随时通过多个应用程序访问该服务。重要的是,它对所有调用它的应用程序做出响应,而它本身除了等待为其他应用程序提供服务之外,并没有什么意义。

Just something I would consider as well when making my decision.

只是我在做决定时也会考虑的事情。

回答by AlexDrenea

I would design an application as a service if the applcation has a critical purpose and should never (or rarely) be closed. Windows services provide good crash-recovery options, good notifications (see the recovery tab in service property).

如果应用程序具有关键目的并且不应该(或很少)关闭,我会将应用程序设计为服务。Windows 服务提供了良好的崩溃恢复选项和良好的通知(请参阅服务属性中的恢复选项卡)。

Also a good reason to use services is because they can run under any user ( so if you deploy them on a server that you remote to, you can safely log out after starting the service without worring that the application will close too).

使用服务的另一个好理由是,它们可以在任何用户下运行(因此,如果将它们部署在远程服务器上,则可以在启动服务后安全地注销,而不必担心应用程序也会关闭)。

I also design services in combination with a desktop application that can interact with the service and can be used to monitor or reconfigure the service while running. This way you can have all the benefits of a tray application, in your service.

我还结合桌面应用程序设计服务,该应用程序可以与服务交互,并可用于在运行时监视或重新配置服务。通过这种方式,您可以在您的服务中获得托盘应用程序的所有好处。

However, you should not abuse using services and only use them, as I said, for cirtical applications.

但是,您不应滥用服务,而应仅将它们用于关键应用程序,正如我所说。