Then using that template, add your code to the desired events of the service. In one you can start a new process with the console app, and in another dll you can just put the functionality the service has to do. You need to seperate the functionality into a class or classes and launch that via one of two stubs. The console stub or service stub. As its plain to see, when running windows, the myriad services that make up the infrastructure do not and can't directly present console windows to the user.
The service needs to communicate with the user in a non graphical way: via the SCM; in the event log, to some log file etc. The service will also need to communicate with windows via the SCM, otherwise it will get shutdown. It would obviously be acceptable to have some console app that can communicate with the service but the service needs to run independently without a requirement for GUI interaction.
The Console stub can very useful for debugging service behaviour but should not be used in a "productionized" environment which, after all, is the purpose of creating a service. I haven't read it fully but this article seems to pint in the right direction. I use a service class that follows the standard pattern prescribed by ServiceBase , and tack on helpers to easy F5 debugging.
This keeps service data defined within the service, making them easy to find and their lifetimes easy to manage. I normally create a Windows application with the structure below. I don't create a console application; that way I don't get a big black box popping in my face every time I run the app. I stay in in the debugger where all the action is. I use Debug. WriteLine so that the messages go to the output window, which docks nicely and stays visible after the app terminates. I usually don't bother add debug code for stopping; I just use the debugger instead.
If I do need to debug stopping, I make the project a console app, add a Stop forwarder method, and call it after a call to Console. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 3 months ago. Active 8 months ago. Viewed k times. Tomas Tomas Why don't you just create a temporary service project and copy over the bits that make it a service?
You could try Topshelf topshelf-project. You could try the technique described here: einaregilsson. I'm not sure. A very simple top shelf alternative: runasservice.
Show 1 more comment. Active Oldest Votes. I usually use the following techinque to run the same app as a console application or as a service: using System. WriteLine "Press any key to stop Joshua Swain 1 1 gold badge 3 3 silver badges 16 16 bronze badges. VladV VladV 9, 2 2 gold badges 30 30 silver badges 46 46 bronze badges. You use ServiceInstaller class, see msdn.
That's expected - your service would run as a separate process so it would be shown in the task manager , but this process would be controlled by the system e. If you run it as a console app, you won't see a service. The whole purpose of this code is to enable you to run it either as a console app, or as a service.
To run as a service you need to install it first using ServiceInstaller class - see MSDN link above - or installuitil. ServiceInstaller is just a utility class to deal with Windows services a little bit like installutil.
You could use it to install whatever you want as a service, the OS doesn't care about the project type you use. Just add a reference in your project to System. ServiceProcess and you'll be able to use the code above — danimal.
Show 7 more comments. Example: HostFactory. Start ; s. RunAsLocalSystem ; x. Last Updated: March 29, Tested. To create this article, volunteer authors worked to edit and improve it over time. The wikiHow Tech Team also followed the article's instructions and verified that they work. This article has been viewed 40, times. Learn more Windows Services also known as Services, services. It is most commonly used to troubleshoot problems, but it must be accessed first.
Log in Social login does not work in incognito and private browsers. Please log in with your username or email to continue. No account yet? Create an account. Edit this Article. We use cookies to make wikiHow great. By using our site, you agree to our cookie policy.
Cookie Settings. Learn why people trust wikiHow. Download Article Explore this Article Steps. Tips and Warnings. To get meaningful information for debugging, the Visual Studio debugger needs to find symbol files for the binaries that are being debugged.
If you are debugging a service that you built in Visual Studio, the symbol files. If you are debugging a service that you didn't build, you should first find symbols for the service and make sure they can be found by the debugger. See Specify Symbol. If you're debugging a system process or want to have symbols for system calls in your services, you should add the Microsoft Symbol Servers.
See Debugging Symbols. Install your service. For more information, see How to: Install and Uninstall Services. For more information, see How to: Start Services. On the menu bar, choose Attach to Process from the Debug or Tools menu. In the Available Processes section, choose the process for your service, and then choose Attach. Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints.
Also, see Troubleshooting: Debugging Windows Services. Attaching to the service's process allows you to debug most, but not all, the code for that service. For example, because the service has already been started, you cannot debug the code in the service's OnStart method or the code in the Main method that is used to load the service this way.
One way to work around this limitation is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this dummy service to load the service process. Once the temporary service has started the process, you can use the Debug menu in Visual Studio to attach to the service process.
Try adding calls to the Sleep method to delay action until you're able to attach to the process. Try changing the program to a regular console application. To do this, rewrite the Main method as follows so it can run both as a Windows Service and as a console application, depending on how it's started.
0コメント