C# 在windows server 2003中将控制台应用程序作为windows服务安装

C# 在windows server 2003中将控制台应用程序作为windows服务安装,c#,C#,这可能是一个基本问题,所以请提前道歉 我想在WindowsServer2003上测试一个控制台应用程序 我在C#with 4.0框架的发布模式下构建了该应用程序,并将bin文件夹的内容粘贴到WindowsServer2003目录下的一个文件夹中 运行exe时,出现以下错误: 无法从命令行或调试器启动服务。必须先安装windows服务(使用installutil.exe),然后使用ServerExplorer启动 现在我想使用installutil.exe作为服务安装这个控制台应用程序 谁能告诉我

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

我想在WindowsServer2003上测试一个控制台应用程序

我在C#with 4.0框架的发布模式下构建了该应用程序,并将bin文件夹的内容粘贴到WindowsServer2003目录下的一个文件夹中

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

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

谁能告诉我怎么做

多谢各位

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

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


另一个选项是用来安排系统运行控制台应用程序。这与服务的行为非常相似,而不需要实际创建服务,因为您可以安排应用程序按您选择的任何计划运行。

您可以更改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时发生了什么?我收到了安装确认消息。但我无法在组件服务中看到我的服务。您将不会在组件服务中看到您的服务,而只是在
服务中看到您的服务。开始->运行->键入服务。msc->按Enter键。如果安装正确,您的服务应该在这里。当您从控制台运行应用程序时会发生什么?如果它立即运行并退出,当您将其作为服务安装时,同样的情况也会发生。服务将启动,然后立即停止,因为应用程序已完成。它不会因为您将其作为服务安装而一直运行。@Abhinav:我确实查看了Services.msc,但找不到我的服务。我查看了日志文件,它提到了以下内容:“在中找不到具有RunInstallerAttribute.Yes属性的公共安装程序”。我的应用程序是作为控制台应用程序而不是windows服务应用程序构建的。