简体   繁体   中英

installing a console application as a windows service in windows server 2003

This might be a basic question so apologies in advance.

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

I built the application on Release mode in C# with 4.0 framework, and have taken the contents of the bin folder and pasted to a folder on windows server 2003 directory.

When I run the exe I get the following error: "Cannot start service from the command line or a debugger. A windows service must first be installed(using installutil.exe) and then started with the ServerExplorer, ...."

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

Can anyone please show me how.

Thank you.

You change 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 new Windows Service(MainService) and 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);
    }
}

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

You need to convert it to a Windows Service application instead of a console application. See the Walkthrough: Creating a Windows Service on MSDN for details.

The other option would be to use Windows Task Scheduler to schedule the system to run your Console Application. This can behave very similarly to a service without actually creating a service, as you can schedule the application to run on any schedule you choose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM