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

Azi*_*shi 4 c#

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

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

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

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

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

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

谢谢.

ram*_*cay 5

你改变Main方法;

static partial class Program
{
    static void Main(string[] args)
    {
        RunAsService();
    }

    static void RunAsService()
    {
        ServiceBase[] servicesToRun;
        servicesToRun = new ServiceBase[] { new MainService() };
        ServiceBase.Run(servicesToRun);
    }
}
Run Code Online (Sandbox Code Playgroud)

并创建新的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)