0

I have a ConsoleApp but I think to move it into a WinSvc application base. And I consider to move the soruce code as a library project and use it in both the old Console to TEST for other possible developments purpose and in the WinSvc for the real WORK area. Thus, when I add something new to the base library It will cause to appear in both apps. And It will be able to make my tests in the ConsoleApp environment and there will only need to rebuild for WinSvc part for real environment (It is to avoid to use the attached debug mode).

So, What can you say about this model? Or Is this a good approach? Or do you have any other suggestion?

Emre Guldogan
  • 590
  • 9
  • 19

1 Answers1

0

This can be a solution:

public static void Main(string[] args)
{
    MyWinSvcClass mySvc = new MyWinSvcClass();

    if (Environment.UserInteractive) 
    {
            // If the executable is started on console
            mySvc.RunAsConsole(args);
    }
    else
    {
            // If the executable is started as a service
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { mySvc };
            ServiceBase.Run(ServicesToRun);
    }
}

Define RunAsConsole() In your service class:

public void RunAsConsole(string[] args)
{
    Log("Service is started on console:");

    OnStart(args);

    Log("Service session is ended.");

    OnStop();
}
Emre Guldogan
  • 590
  • 9
  • 19