在Windows server 2003中安装控制台应用程序作为Windows服务

本文关键字:Windows 应用程序 服务 控制台 安装 server 2003 | 更新日期: 2023-09-27 18:16:33

这可能是一个基本问题,所以提前道歉。

我有一个控制台应用程序,我想在windows server 2003上测试。

我使用c# 4.0框架在Release模式下构建应用程序,并将bin文件夹中的内容粘贴到windows server 2003目录下的一个文件夹中。

当我运行exe时,我得到以下错误:"无法从命令行或调试器启动服务。必须首先安装windows服务(使用installutil.exe),然后使用ServerExplorer ....启动"

现在我想安装这个控制台应用程序使用installutil.exe作为一个服务。

谁能告诉我怎么做?

谢谢。

在Windows server 2003中安装控制台应用程序作为Windows服务

更改Main方法;

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

创建新的Windows服务(MainService)和安装程序类(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);
    }
}

现在我想安装这个控制台应用程序使用installutil.exe作为一个服务。

您需要将其转换为Windows服务应用程序而不是控制台应用程序。有关详细信息,请参阅演练:在MSDN上创建Windows服务。

另一个选择是使用Windows任务调度程序来调度系统运行控制台应用程序。这与服务的行为非常相似,而无需实际创建服务,因为您可以将应用程序安排为按照您选择的任何时间表运行。