Windows服务开始,然后使用Topshelf停止 [英] Windows Service started and then stopped using Topshelf

查看:2422
本文介绍了Windows服务开始,然后使用Topshelf停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Quartz.net,我试图让石英服务器开始关闭在Windows服务。我创建了一个Windows服务项目,其中包括了Quartz.net库。在我服务类我有:

I am using Quartz.net and I am trying to get the Quartz server to start-off in a Windows Service. I have created a Windows Service Project and included the Quartz.net libraries. 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);
    }

}



我也创建一个Windows服务安装并成功安装Windows服务在Visual Studio的命令提示符下使用:

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

installutil MyWindowsService.exe

当我在Windows服务列表中查看我的服务,并尝试启动服务 - 我得到一个消息对话框:

When I view my service in the Windows service list and try to start the service - I get a message dialog box:

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的):

Here is the output I have logged to the event viewer (log4net):

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

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

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

有谁知道我能得到这个服务启动没有这个错误(S)被抛出?

Does anyone know how I can get this service to start without this error(s) being thrown?

先谢谢了。

推荐答案

我创建了一个Windows服务项目......我也创建一个Windows服务安装,并已成功
使用安装在Windows服务在Visual Studio的命令提示符:
installutil MyWindowsService.exe

Topshelf服务已根据ServiceBase,做自己的安装 - 你有,你可以随着运行控制台应用程序你应用程序开发中,看看它的工作,那么当你要安装它作为你去一个命令提示符作为管理员,并调用 MyWindowsService.exe安装服务 - 见<一HREF =http://docs.topshelf-project.com/en/latest/overview/commandline.html>文档的所有选项。它可能工作包在另一项服务,但我不明白你为什么会想这样做。

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. It may work wrapped in another service, but I don't see why you'd want to do so.

有一个功能服务的in的文档

如果你这样做需要一个安装程序,有一处的 http://www.bjoernrochel.de/2010/01/09/how-to-integrate-a-topshelf-based-service-with-vs-setup-projects/ (但Topshelf的命令行语法。已经改变,因为这是书面的,它需要更新)

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

(编辑:我只注意到事件序号3和4包含文本请运行'MyWindowsService ..安装')

( I just noticed that events number 3 and 4 contain the text "Please run 'MyWindowsService .. install')

这篇关于Windows服务开始,然后使用Topshelf停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆