[英]installing a console application as a windows service in windows server 2003
This might be a basic question so apologies in advance. 这可能是一个基本问题,所以请提前道歉。
I have a console application that I want to test on a windows server 2003. 我有一个控制台应用程序,我想在Windows Server 2003上测试。
I built the application on Release mode in C# with 4.0 framework, and have taken the contents of the bin folder and pasted to a folder on windows server 2003 directory. 我在C#的4.0框架下在Release模式下构建了应用程序,并将bin文件夹的内容粘贴到windows server 2003目录下的文件夹中。
When I run the exe I get the following error: "Cannot start service from the command line or a debugger. A windows service must first be installed(using installutil.exe) and then started with the ServerExplorer, ...." 当我运行exe时,我收到以下错误:“无法从命令行或调试器启动服务。必须首先安装Windows服务(使用installutil.exe),然后启动ServerExplorer,....”
Now I want to install this console app using the installutil.exe as a service. 现在我想使用installutil.exe作为服务来安装此控制台应用程序。
Can anyone please show me how. 任何人都可以告诉我如何。
Thank you. 谢谢。
You change Main method; 你改变Main方法;
static partial class Program
{
static void Main(string[] args)
{
RunAsService();
}
static void RunAsService()
{
ServiceBase[] servicesToRun;
servicesToRun = new ServiceBase[] { new MainService() };
ServiceBase.Run(servicesToRun);
}
}
And you create new Windows Service(MainService) and Installer Class(MyServiceInstaller); 并创建新的Windows服务(MainService)和安装程序类(MyServiceInstaller);
MainService.cs; 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; 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);
}
}
Now I want to install this console app using the installutil.exe as a service.
现在我想使用installutil.exe作为服务来安装此控制台应用程序。
You need to convert it to a Windows Service application instead of a console application. 您需要将其转换为Windows服务应用程序而不是控制台应用程序。 See the Walkthrough: Creating a Windows Service on MSDN for details.
有关详细信息,请参阅演练:在MSDN上创建Windows服务 。
The other option would be to use Windows Task Scheduler to schedule the system to run your Console Application. 另一种选择是使用Windows任务计划程序来安排系统运行您的控制台应用程序。 This can behave very similarly to a service without actually creating a service, as you can schedule the application to run on any schedule you choose.
这可以与没有实际创建服务的服务非常相似,因为您可以安排应用程序在您选择的任何计划上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.