简体   繁体   English

为什么安装后我的服务无法启动?

[英]Why doesn't my service start after installation?

I've created a windows service, which is set to start automatically. 我已经创建了Windows服务,该服务设置为自动启动。 I also added following code to installer: 我还向安装程序添加了以下代码:

    public ProjectInstaller()
    {
        InitializeComponent();
        serviceProcessInstaller1.AfterInstall += new InstallEventHandler(serviceProcessInstaller1_AfterInstall); 
    }

    void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {
        try
        {
            var sc = new ServiceController(serviceInstaller1.ServiceName);
            sc.Start();
        }
        catch
        {
        }
    }

    protected override void OnCommitted(IDictionary savedState)
    {
        try
        {
            var sc = new ServiceController(serviceInstaller1.ServiceName);
            sc.Start();
        }
        catch
        {
        }
    }

The service is installed properly, but it never starts. 该服务已正确安装,但从未启动。

What could be the cause of this? 这可能是什么原因?

Perhaps you need to put in some temporary diagnostic logging, maybe using System.IO.File.WriteAllText(); 也许您需要使用System.IO.File.WriteAllText();进行一些临时的诊断日志记录System.IO.File.WriteAllText(); . I know it's not the answer you're looking for, but it'll probably give you the quickest solution! 我知道这不是您要找的答案,但是它可能会为您提供最快的解决方案!

try
{
    var sc = new ServiceController(serviceInstaller1.ServiceName);
    sc.Start();
    System.IO.File.WriteAllText(@"c:\temp\servicestart.txt", "Service started");
}
catch (Exception ex)
{
    System.IO.File.WriteAllText(@"c:\temp\servicestart.txt", ex.Message);
}

Ensure in your Main() function that you have this line: 确保在Main()函数中具有以下行:

ServiceBase.Run(new ServiceClass());

I've been guilty a few times of leaving Application.Run(new Class()); 我几次离开Application.Run(new Class());感到内 (If you started off of a windows form app) (如果您是从Windows窗体应用程序开始的)

I've created a service a while ago and what mine differs from yours is that you declared this way 我不久前创建了一个服务,而我的服务与您的服务不同之处在于您这样声明

var sc = new ServiceController(serviceInstaller1.ServiceName);

mine instead of taking the serviceInstaller1.ServiceName I was giving the name via a simple string like this 我的而不是采用serviceInstaller1.ServiceName我是通过一个像这样的简单字符串给出名称的

var sc = new ServiceController("MyService");

I think this isn't the problem at all, but when talking about services everything is worth a try 我认为这根本不是问题,但是在谈论服务时,一切都值得尝试

EDIT: giving a look at it now I've seen that the name I used was actually the DisplayName not the service name, try passing it manually or by using serviceInstaller1.DisplayName 编辑:现在来看一下,我已经看到我使用的名称实际上是DisplayName而不是服务名称,请尝试手动将其传递或通过使用serviceInstaller1.DisplayName

Does it possibly have a dependency on a another service. 它是否可能依赖于另一个服务。 Or have you tried a delayed start? 还是您尝试过延迟启动?

This worked for me 这对我有用

protected override void OnAfterInstall(IDictionary savedState)
{
      base.OnAfterInstall(savedState);
      System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceInstaller1.ServiceName);
      sc.Start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM