Adding a console using program logic

I have a windows service. If I started it from the debugger, I want to start console output (since you cannot start the service).

Typically, a Windows service is installed in WindowApplication as a project type and does not have a window entry point. This removes the old console.

If you want a console window, you need to change the project type to ConsoleAppication. I would like to do this inside the program itself, changing the project settings instead.

Is it possible?

+3
source share
5 answers

In fact, you can use a simple check when a 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 a sample code.

namespace TestService { static class Program { [DllImport("kernel32.dll")] static extern bool AllocConsole(); /// <summary> /// The main entry point for the application. /// </summary> static void Main() { if (!Environment.UserInteractive) { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } else { AllocConsole(); //Start Code that interfaces with console. } } } } 
+2
source

You can use AllocConsole API

 [DllImport("kernel32.dll")] static extern bool AllocConsole(); 

Use FreeConsole to disconnect the console from your process:

 [DllImport("kernel32.dll")] static extern bool FreeConsole(); 
+2
source

I usually develop any program as a class library (or a set of libraries) with a logical entry point, then add the shell of the startup project: a console application, a Windows service, a website.

If you have an entry point in your program (a class with a method that runs all your business logic), you can create it as a class library without any changes and add a console project and a Windows service project to your solution, which is basically a class (for example, Program.cs) instantiates an entry point and calls an input method.

This approach does not affect your business logic using the approach and allows you to create every use method every time you build an entire solution. In other words, this allows us to separate the problems: the program and how to run it.

+1
source

It is good practice to have two programs (i.e., two projects in Visual Studio that produce executable files, plus one or more projects for the overall application logic):

  • One for a variant of your Windows software.
  • Another option for the console version of your software.

The advantage is that you can freely choose whether to run the software as a service or in console mode. For example, when starting in console mode, on systems such as Log4Net, you can configure log output to the console, which helps diagnose problems in production environments.

0
source

Yes, you can do this in several ways. I am using the following solution:

  • Create a console application project. Call it Console.Service or something else.

  • Go to your class of service and create the following code:

     private static void Main() { #if !DEBUG var servicesToRun = new [] { new DemoService() }; Debug.WriteLine("Run service..."); Run(servicesToRun); #else DemoService service = new DemoService(); service.OnStart(null); Console.WriteLine("Press ENTER to quit..."); Console.ReadLine(); service.OnStop(); #endif } 
  • Add an existing item to the console application project by linking it 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 are done. As I said, there are many ways to go. Choose the one you like best.

0
source

Source: https://habr.com/ru/post/906934/


All Articles