Can I develop a windows service, that can be both a windows service and a (<any type>) application?

∥☆過路亽.° 提交于 2021-02-08 08:32:16

问题


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 ?


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 ?

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)


回答1:


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

  • check if(Environment.UserInteractive), or
  • check the command line arguments, for example a "-console" switch

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.




回答2:


We use the command-line technique described by Marc Gravell in our service applications, so be sure to try it.

Note that you will be able to easily debug your application running as a true service on Windows XP and 2003, where you can interactively log into Session 0 and attach your debugger to the running service application. "Session 0 isolation", introduced in Windows Vista, makes it difficult to interact with/debug service applications so we end up doing our first round of testing and debugging on XP.

Good luck.




回答3:


You're kind of on the right track when you talk about building a "helper application." You can build all the real logic of your application into a class library (dll) project, and then write a windows service that just calls the library.

Then you can write other applications, say a console app or unit tests which reference your class library.

That way you can write any front end application you want, and it just interfaces to the class library. That's also how you do unit tests, which would let you test most of the functionality without even really running the application... but that's really a bigger topic. :)



来源:https://stackoverflow.com/questions/4525450/can-i-develop-a-windows-service-that-can-be-both-a-windows-service-and-a-any

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!