我可以开发Windows服务,该服务既可以是Windows服务又可以是(<任何类型>)应用程序? [英] Can I develop a windows service, that can be both a windows service and a (<any type>) application?

查看:40
本文介绍了我可以开发Windows服务,该服务既可以是Windows服务又可以是(<任何类型>)应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想创建Windows服务,但是想调试它(可能还有其他原因……)我想创建一个可以作为Windows服务或应用程序构建的应用程序.我该怎么办?

Suppose I want to create a windows service, but as I want to debug it, (and there may be other reasons...) I want to create an application that can be build as a windows service OR application. How do i do that ?

您能否通过在服务帮助器应用程序中构建服务的功能来做到这一点,说出serviceHelper并实现启动和停止方法以及其中需要的内容?然后,您是否可以仅通过调用刚刚创建的serviceHelper中的方法来创建一个非常小的Windows服务(启动和停止,等等)来实现,然后以这种方式使用serviceHelper调试服务?

Can you do that by Building the functionality of your service in a service helper application, say serviceHelper and implement start and stop methods and whatever you need in there ? Can you then create a very very small windows-service who's start and stop, etc you need are implemented by just calling the methods in the serviceHelper you just created ?, and this way debug your service using your serviceHelper ?

这确实是可行的,除了您应该考虑无法调试projectInstaller的问题之外?(serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic)(serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService)

Is it true that this works, except for the fact that you should take into account that you cant debug problems with the projectInstaller ? ( serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic) (serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService)

推荐答案

如果您使用标准服务样板,但是将 Main()编辑为以下任何一个:

If you use the standard service boilerplate, but then edit Main(), to either:

  • 检查 if(Environment.UserInteractive),或
  • 检查命令行参数,例如" -console "开关

并更改代码以直接运行代码 而不是运行标准的服务启动代码,它应该可以正常工作.

and change the code to run your code directly instead of running the standard service startup code, it should work fine.

有关完整示例(包括自我安装/卸载),请参阅我的回答

For a full example (including self install/uninstall), see my answer here.

最大的问题是您的调试是在不同的安全上下文(交互式用户)而不是服务帐户中运行的事实.

The biggest issue is allowing for the fact that your debugging runs in a different security context (the interactive user's) rather than a service account.

这篇关于我可以开发Windows服务,该服务既可以是Windows服务又可以是(<任何类型>)应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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