gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:05:59 25 4
gpt4 key购买 nike

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

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

我使用 4.0 框架在 C# 中以 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);

主服务.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();
}
}

我的服务安装程序.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);
}
}

关于c# - 在 Windows Server 2003 中将控制台应用程序安装为 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16903546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com