2013-06-03 83 views
4

这可能是一个基本问题,因此提前致歉。在Windows Server 2003中安装控制台应用程序作为Windows服务

我有,我想测试Windows Server 2003的

我建在C#中发布模式的应用与4.0框架上的控制台应用程序,并已采取bin文件夹中的内容并粘贴到在Windows Server 2003目录下的文件夹。

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

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

任何人都可以请告诉我如何。

谢谢。

+0

那么运行InstallUtil时发生了什么? – nvoigt

+0

我收到了已安装的确认消息。但是我在组件服务中看不到我的服务。 –

+1

您不会在组件服务中看到您的服务,而只是在“服务”中。开始 - >运行 - >输入Services.msc->按Enter键。这就是你的服务应该在哪里,如果安装正确。 – Abhinav

回答

0

Now I want to install this console app using the installutil.exe as a service.

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

另一种选择是使用Windows Task Scheduler来安排系统运行控制台应用程序。这可以非常类似于服务而不实际创建服务,因为您可以安排应用程序按照您选择的任何时间表运行。

+0

我想他已经有了一个服务,或者他不会首先得到那个错误信息。 – nvoigt

5

您更改主要方法;您可以创建新的Windows服务(MainService)和安装程序类(MyServiceInstaller);您将创建一个新的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); 
    } 
} 
相关问题