【问题标题】:Windows Service started and then stopped using TopshelfWindows 服务使用 Topshelf 启动然后停止
【发布时间】:2012-12-05 14:58:29
【问题描述】:

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

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.. "

有谁知道如何在不引发此错误的情况下启动此服务?

提前致谢。

【问题讨论】:

  • @PeteH 这不是重复的,而不是我的错误实现。 Topshelf的问题和版本也不同。
  • 旧线程但是; ex.InnerException.Message 很容易导致空指针。避免

标签: c# windows-services quartz.net topshelf


【解决方案1】:

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

Topshelf 服务已经基于 ServiceBase 并进行自己的安装 - 您有一个控制台应用程序,您可以在开发中与您的应用程序一起运行以查看它的工作情况,然后在您想要安装它时作为一项服务,您以管理员身份进入命令提示符并调用MyWindowsService.exe install - 有关所有选项,请参见the documentation。它可能包含在另一个服务中,但我不明白你为什么要这样做。

有一个功能服务in the docs的基本示例。

如果您确实需要安装程序,http://www.bjoernrochel.de/2010/01/09/how-to-integrate-a-topshelf-based-service-with-vs-setup-projects/ 有一个(但 Topshelf 的命令行语法在编写后发生了变化,需要更新。)

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

【讨论】:

  • 谢谢。我没有意识到 Quartz 使用的是 Topshelf,现在我将服务包装在服务中是有道理的。命令:MyWindowsService.exe install 运行良好,服务按预期工作。再次感谢。
【解决方案2】:

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

这是该服务在 Windows 服务中注册的名称(您将使用的名称sc start quartzserver)

【讨论】:

  • 我现在正要尝试。您知道 Quartz.net 项目中的其他任何地方是否引用了该服务名称吗?一会儿我会更新。
  • 抱歉,我不知道,因为我以前从未使用过 Quartz.NET,我只是根据我在 TopShelf 遇到的类似问题提出这个想法。
【解决方案3】:

当我有不同的服务名称和显示名称值时,我收到了这个错误。 我有“Auditing.Service”和“Auditing Service”,我将它们都更改为“Auditing.Service”并启动了 Windows 服务。

所以我的建议是:让服务和显示名称匹配。

【讨论】:

    【解决方案4】:

    如果我正确读取了日志,则看起来Topshelf.HostConfigurators.WindowsServiceDescription 服务要么未安装,要么未运行。

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

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

    【讨论】:

    • 我之前检查过这个Topshelf.HostConfigurators.WindowsServiceDescription 服务,但找不到任何关于它的问题,甚至找不到如何启动它。
    • 我删除了句号,重新部署并测试了它,不幸的是它仍然完全一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    相关资源
    最近更新 更多