使用Topshelf启动的Windows服务启动后立即停止

6

我正在使用Quartz.net,并尝试让Quartz服务器在Windows服务中启动。我创建了一个Windows服务项目并包含了Quartz.net库。在我的Service类中,我有:

protected override void OnStart(string[] args)
{
    try
    {
        Host host = HostFactory.New(x =>
        {
            x.Service<IQuartzServer>(s =>
            {
                s.SetServiceName("quartz.server");
                s.ConstructUsing(builder =>
                {
                    QuartzServer server = new QuartzServer();
                    server.Initialize();
                    return server;
                });
                s.WhenStarted(server => server.Start());
                s.WhenPaused(server => server.Pause());
                s.WhenContinued(server => server.Resume());
                s.WhenStopped(server => server.Stop());
            });

            x.RunAsLocalService();
            //x.RunAs(@"mydomain\mysusername", "mypassword");

            x.SetDescription(Configuration.ServiceDescription);
            x.SetDisplayName(Configuration.ServiceDisplayName);
            x.SetServiceName(Configuration.ServiceName);
        });

        host.Run();
    }
    catch (Exception ex)
    {
        Log.Error(ex.Message);
        Log.Error(ex.InnerException.Message);
    }

}

我还创建了一个Windows服务安装程序,并使用以下命令在Visual Studio的命令提示符中成功安装了Windows服务:

installutil MyWindowsService.exe

当我在Windows服务列表中查看我的服务并尝试启动服务时,会弹出消息对话框:

The MyWindowsService service on Local Computer started and the
stopped. Some Services stop automatically if they are not in use by
other services or programs.

这里是我记录在事件查看器(log4net)中的输出:

Windows 事件

1

Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
14:52:24,044 [11528] INFO 
Common.Logging.Factory.AbstractLogger.Info(:0) - Finished Starting
MyProject Windows Service."

2

Error   05/12/2012 14:52    Service1    "Service cannot be started.
System.NullReferenceException: Object reference not set to an instance
of an object.    at MyWindowsService.MyProject.OnStart(String[] args)
in c:\My Projects\MyProject
v40\CO40\MyWindowsService\MyProject.cs:line 58    at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)"

3

Error   05/12/2012 14:52    MyWindowsService.exe    "2012-12-05 14:52:24,042
[6048] ERROR Common.Logging.Factory.AbstractLogger.Error(:0) - The
Topshelf.HostConfigurators.WindowsServiceDescription service has not
been installed yet. Please run 'MyWindowsService, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null install'. "

4

Error   05/12/2012 14:52    MyWindowsService.exe    "2012-12-05 14:52:24,041
[6048] FATAL Topshelf.Windows.WindowsServiceHost.Run(:0) - The
Topshelf.HostConfigurators.WindowsServiceDescription service has not
been installed yet. Please run 'MyWindowsService, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null install'. "

5

Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
14:52:24,039 [6048] INFO  Topshelf.Windows.WindowsServiceHost.Run(:0)
- Starting up as a winservice application "

6

Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
14:52:24,038 [6048] DEBUG Topshelf.Builders.RunBuilder.CreateHost(:0)
- Running as a Windows service, using the service host "

7

Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
14:52:24,027 [6048] INFO  Topshelf.OS.OsDetector.DetectOs(:0) -
Detected the operating system: 'win' "

8

Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
14:52:23,895 [6048] INFO 
Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost(:0) -
Topshelf v2.2.2.0, .NET Framework v4.0.30319.17929 "

9

Information 05/12/2012 14:52    MyWindowsService.exe    "2012-12-05
14:52:23,829 [11528] INFO 
Common.Logging.Factory.AbstractLogger.Info(:0) - Starting MyProject
Windows Service.. "

有人知道我怎样才能让这个服务在不抛出错误的情况下启动吗?

提前感谢。


dup?http://stackoverflow.com/questions/7351973/working-with-topshelf-im-running-into-an-error-around-topshelf-hostconfigurat - PeteH
@PeteH 这不是重复问题,而是我自己实现的问题。这是一个不同的问题,也是 Topshelf 的不同版本。 - Seany84
旧的线程,但是ex.InnerException.Message很容易导致空指针。请避免。 - mugume david
4个回答

10

我创建了一个Windows Service项目...我还创建了一个Windows Service安装程序,并成功地使用Visual Studio的命令提示符安装了Windows Service:installutil MyWindowsService.exe

Topshelf服务已经基于ServiceBase并且自己完成了安装 - 您有一个控制台应用程序,可以在开发中与您的应用程序一起运行以查看它是否工作,然后当您想要将其安装为服务时,您需要以管理员身份转到命令提示符并调用MyWindowsService.exe install - 有关所有选项,请参见文档。它可能包含在另一个服务中运行,但我不知道为什么您要这样做。

文档中提供了一个功能服务的基本示例..

如果您确实需要安装程序,则可在这里下载一个(但自那以后,Topshelf的命令行语法已更改,需要更新)。

(编辑:我刚注意到事件3和4包含文本“请运行'MyWindowsService.. install'”)


谢谢。我没有意识到Quartz正在使用Topshelf,现在我明白了,我是在一个服务中包装另一个服务。命令:MyWindowsService.exe install完美地工作了,服务按预期工作。再次感谢。 - Seany84

0

尝试从s.SetServiceName("quartz.server");中删除.。当我使用的SetServiceName值不仅仅是a-z字符时,我遇到了麻烦。

这是在Windows服务中注册的名称(您将用它来执行sc start quartzserver)。


我现在就要尝试一下。你知道这个服务名称是否在Quartz.net项目的其他地方被引用吗?我稍后会更新。 - Seany84
抱歉,我不清楚Quartz.NET的使用情况,我只是根据我之前在TopShelf上遇到的类似问题来推测。 - Trevor Pilley

0
当我拥有不同的服务名称和显示名称时,我遇到了这个错误。我拥有"Auditing.Service"和"Auditing Service",然后我将它们都更改为"Auditing.Service",Windows服务就开始运行了。
因此,我的建议是:使服务名称和显示名称匹配起来。

-1

如果我正确地阅读日志,看起来 Topshelf.HostConfigurators.WindowsServiceDescription 服务未安装或未运行。

你在 c:\My Projects\MyWindowsService\Service1.cs 的第58行也有一个NRE。这可能与先前的错误有关。

你的下一个选择是直接向 TopShelf 社区寻求帮助:http://topshelf-project.com/contact/


我之前检查过 Topshelf.HostConfigurators.WindowsServiceDescription 服务,但没有发现任何关于它的问题或如何启动它的信息。 - Seany84
我已经移除了句号,重新部署并测试了它,但遗憾的是结果仍然完全相同。 - Seany84

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接