简体   繁体   English

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

[英]Windows Service started and then stopped using Topshelf

I am using Quartz.net and I am trying to get the Quartz server to start-off in a Windows Service. 我正在使用Quartz.net,我试图让Quartz服务器在Windows服务中启动。 I have created a Windows Service Project and included the Quartz.net libraries. 我创建了一个Windows服务项目并包含了Quartz.net库。 In my Service class I have: 在我的服务类中,我有:

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);
    }

}

I have also created a Windows Service Installer and have successfully installed the Windows Service in Visual Studio's command prompt using: 我还创建了一个Windows服务安装程序,并使用以下命令在Visual Studio的命令提示符中成功安装了Windows服务:

installutil MyWindowsService.exe installutil MyWindowsService.exe

When I view my service in the Windows service list and try to start the service - I get a message dialog box: 当我在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.

Here is the output I have logged to the event viewer (log4net): 这是我记录到事件查看器(log4net)的输出:

Windows Events Windows事件

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

Does anyone know how I can get this service to start without this error(s) being thrown? 有没有人知道如何在没有抛出此错误的情况下启动此服务?

Thanks in advance. 提前致谢。

I have created a Windows Service Project ... I have also created a Windows Service Installer and have successfully installed the Windows Service in Visual Studio's command prompt using: installutil MyWindowsService.exe 我创建了一个Windows服务项目...我还创建了一个Windows服务安装程序,并使用以下命令在Visual Studio的命令提示符下成功安装了Windows服务:installutil MyWindowsService.exe

Topshelf services are already based on ServiceBase and do their own installation - you have a console application which you can run along with your app in development to see it's working, then when you want to install it as a service you go to a command prompt as an administrator and call MyWindowsService.exe install - see the documentation for all options. Topshelf服务已基于ServiceBase并进行自己的安装 - 您有一个控制台应用程序,您可以在开发中与您的应用程序一起运行以查看它是否正常工作,然后当您要将其作为服务安装时,您将转到命令提示符管理员并调用MyWindowsService.exe install - 请参阅所有选项的文档 It may work wrapped in another service, but I don't see why you'd want to do so. 它可能包含在另一个服务中,但我不明白你为什么要这样做。

There's a basic example of a functional service in the docs . 在文档中有一个功能服务的基本示例。

If you do need an installer, there's one at http://www.bjoernrochel.de/2010/01/09/how-to-integrate-a-topshelf-based-service-with-vs-setup-projects/ (but Topshelf's command line syntax has changed since that was written, and it needs updating.) 如果你确实需要一个安装程序,可以访问http://www.bjoernrochel.de/2010/01/09/how-to-integrate-a-topshelf-based-service-with-vs-setup-projects/ (但是Topshelf的命令行语法自编写以来发生了变化,需要更新。)

(Edit: I just noticed that events number 3 and 4 contain the text "Please run 'MyWindowsService .. install') (编辑:我刚注意到3号和4号事件包含文本“请运行'MyWindowsService .. install')

Try removing the . 尝试删除. from s.SetServiceName("quartz.server"); 来自s.SetServiceName("quartz.server"); I had trouble when the value I used for SetServiceName wasn't just az characters only. 当我用于SetServiceName的值不仅仅是az字符时,我遇到了麻烦。

That's the name that the service is registered as in windows service (the name you'd use to do sc start quartzserver ) 这是服务在Windows服务中注册的名称(用于sc start quartzserver

I was getting this error when I had different Service Name and Display Name values. 当我有不同的服务名称和显示名称值时,我收到此错误。 I had "Auditing.Service" and "Auditing Service", and I changed them both to "Auditing.Service" and the Windows service started. 我有“Auditing.Service”和“审核服务”,我将它们都更改为“Auditing.Service”并启动了Windows服务。

So my suggestion is: make the service and display names match up. 所以我的建议是:使服务和显示名称匹配。

If i'm reading the logs correctly, it looks like the Topshelf.HostConfigurators.WindowsServiceDescription service is either not installed, or not running. 如果我正确读取日志,看起来Topshelf.HostConfigurators.WindowsServiceDescription服务未安装或未运行。

you also have a NRE on line 58 of c:\\My Projects\\MyWindowsService\\Service1.cs . 您还在c:\\My Projects\\MyWindowsService\\Service1.cs第58行上有一个NRE。 This may be related to the previous error. 这可能与先前的错误有关。

your next option is to ask for help from the TopShelf community directly: http://topshelf-project.com/contact/ 您的下一个选择是直接向TopShelf社区寻求帮助: http ://topshelf-project.com/contact/

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

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