Installing the console application as a Windows service on Windows Server 2003

This may be the main question, so apologize in advance.

I have a console application that I want to test on a Windows 2003 server.

I created the application in Release mode in C # with framework 4.0 and took the contents of the bin folder and inserted it into the folder in the Windows Server 2003 directory.

When I start exe, I get the following error: "Cannot start the service from the command line or debugger. First you need to install the Windows service (using installutil.exe), and then start from ServerExplorer, ..."

Now I want to install this console application using installutil.exe as a service.

Can someone show me how.

Thanks.

+4
source share
2 answers

You change the Main method;

static partial class Program { static void Main(string[] args) { RunAsService(); } static void RunAsService() { ServiceBase[] servicesToRun; servicesToRun = new ServiceBase[] { new MainService() }; ServiceBase.Run(servicesToRun); } } 

And you create a new Windows service (MainService) and an installer class (MyServiceInstaller);

MainService.cs;

 partial class MainService : ServiceBase { public MainService() { InitializeComponent(); } protected override void OnStart(string[] args) { base.OnStart(args); } protected override void OnStop() { base.OnStop(); } protected override void OnShutdown() { base.OnShutdown(); } } 

MyServiceInstaller.cs;

 [RunInstaller(true)] public partial class SocketServiceInstaller : System.Configuration.Install.Installer { private ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller; public SocketServiceInstaller() { InitializeComponent(); processInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller(); processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServiceName = "My Service Name"; var serviceDescription = "This my service"; Installers.Add(serviceInstaller); Installers.Add(processInstaller); } } 
+5
source

Now I want to install this console application using installutil.exe as a service.

You need to convert it to a Windows Service application instead of a console application. For more information, see Walkthrough: Creating a Windows Service on MSDN.

Another option is to use the Windows Task Scheduler to schedule system startup in a console application. This can behave very much like a service without actually creating the service, since you can plan to run the application according to any schedule you choose.

0
source

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


All Articles