I have a windows service. If I start it from the debugger I want to run with console output (since you can not run a service).
Usually a Windows Service is set to WindowApplication as project type and has no "window" entry point. Thus it removes the good old console.
If you want a console window you need to change the project type to ConsoleAppication. I would like to do this within the program itself instead changing the Project settings.
Is it possible?
I usually develop any program as a class library (or set of libraries), with a logical entry point, then I add the launcher project wrapper: a console application, a windows service, a web site.
If, in your program, you have an entry point (a class with a method that starts all your business logic), then you can build it as a class library without any changes and add a console project and a windows service project to your solution that, in the main class (e.g. Program.cs) instantiates the entry point and call the entry method.
This approach does not invade your business logic with use approach and lets you build every mode of use every time you build the entire solution. In other words it allows you to separates concerns: the program and how to launch it.
A good practise is having two programs (i.e. two projects in Visual Studio producing executables, plus one or more projects for the shared application logic):
The advantage is you can choose freely whether to run the software as a service or in console mode. For example, when run in console mode, with frameworks like Log4Net you can configure logging output to the console which helps in diagnosing problems in production environments.
You can use the
AllocConsole
APIUse
FreeConsole
to detach the console from your process:Yes you can do it on multiple ways. I use the following solution:
Create a Console Application project. Name it Console.Service or something else.
Go to your service class and create the following code:
Add a existing item to the Console Application project by linking to the Service class from your service project. You do this by clicking on the arrow next to the add button (Add as Link).
You're done. Like I said there are multiple ways to go. Choose the one you feel most happy with.
Actually you can use a simple check when the program starts to see if it is running as a service or not and then use the AllocConsole command to start the console. Here is the sample code.