簡體   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