C# Windows服务已启动,然后停止使用Topshelf

C# Windows服务已启动,然后停止使用Topshelf,c#,windows-services,quartz.net,topshelf,C#,Windows Services,Quartz.net,Topshelf,我正在使用Quartz.net,我正在尝试让Quartz服务器在Windows服务中启动。我创建了一个Windows服务项目,并包括Quartz.net库。在我的服务课上,我有: protected override void OnStart(string[] args) { try { Host host = HostFactory.New(x => { x.Service<IQuartzServer>(

我正在使用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);
    }

}
以下是我记录到事件查看器(log4net)的输出:

Windows事件 一,

二,

三,

四,

五,

六,

七,

八,

九,

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


提前感谢。

如果我正确阅读了日志,则可能是
Topshelf.HostConfigurators.WindowsServiceDescription
服务未安装或未运行

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


您的下一个选择是直接向TopShelf社区寻求帮助:

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

这是服务在windows服务中注册的名称(您将用于执行
sc start quartzserver

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

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

这里有一个功能性服务的基本示例

如果您确实需要安装程序,在上有一个(但Topshelf的命令行语法自编写以来已经更改,需要更新)


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

当我有不同的服务名称和显示名称值时,我遇到了此错误。 我有“Auditing.Service”和“Auditing.Service”,我把它们都改为“Auditing.Service”,Windows服务启动了


因此,我的建议是:使服务和显示名称匹配。

我以前检查过这个
Topshelf.HostConfigurators.WindowsServiceDescription
服务,但找不到任何关于它的问题,甚至找不到如何启动它的问题。我删除了句号,重新部署并测试了它,但很遗憾,它仍然是完全相同的.dup?@PeteH这不是一个重复,而我的实现不好。Topshelf的问题和版本也不同。旧线程但是;
ex.InnerException.Message
很容易导致空指针。AvoidI现在就要尝试了。你知道Quartz.net项目中是否引用了该服务名称吗?我会更新过了一会儿。对不起,我不知道,因为我以前从未使用过Quartz.NET,我只是基于我与TopShelf的类似问题提出了这个想法。谢谢。我没有意识到Quartz在使用TopShelf,现在我在服务中包装服务是有意义的。命令:
MyWindowsService.exe install
运行良好,并且服务按预期运行。再次感谢。
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.
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."
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)"
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'. "
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'. "
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 "
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 "
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' "
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 "
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.. "