繁体   English   中英

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

[英]installing a console application as a windows service in windows server 2003

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

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

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

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

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

任何人都可以告诉我如何。

谢谢。

你改变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任务计划程序来安排系统运行您的控制台应用程序。 这可以与没有实际创建服务的服务非常相似,因为您可以安排应用程序在您选择的任何计划上运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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