Windows Service started and then stopped using Topshelf
Asked Answered
I

4

6

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

}

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

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.

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

Windows Events

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

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

Thanks in advance.

Inextirpable answered 5/12, 2012 at 14:58 Comment(3)
dup? #7352473Parasynthesis
@Parasynthesis It was not a duplicate as opposed to a bad implementation on my part. Different problem and version of Topshelf too.Inextirpable
old thread but; ex.InnerException.Message can easily cause a null pointer. AvoidGaloshes
C
10

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

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.

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

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

Cobaltic answered 5/12, 2012 at 20:45 Comment(1)
Thanks. I didn't realise Quartz was using Topshelf and it makes sense now that I was wrapping a service within a service. The command: MyWindowsService.exe install worked perfectly and the service is working as expected. Thanks again.Inextirpable
B
0

Try removing the . from s.SetServiceName("quartz.server"); I had trouble when the value I used for SetServiceName wasn't just a-z characters only.

That's the name that the service is registered as in windows service (the name you'd use to do sc start quartzserver)

Betthezul answered 5/12, 2012 at 15:17 Comment(2)
I'm about to try that now. Do you know if that service name is referenced anywhere else in the Quartz.net project? I'll update in a little while.Inextirpable
Sorry, I have no idea as I've never used Quartz.NET before, I'm just basing the idea upon the similar issue I had with TopShelf.Betthezul
D
0

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.

So my suggestion is: make the service and display names match up.

Despairing answered 26/2, 2019 at 11:17 Comment(0)
A
-1

If i'm reading the logs correctly, it looks like the Topshelf.HostConfigurators.WindowsServiceDescription service is either not installed, or not running.

you also have a NRE on line 58 of c:\My Projects\MyWindowsService\Service1.cs. 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/

Aftermost answered 5/12, 2012 at 15:6 Comment(2)
I have previously checked for this Topshelf.HostConfigurators.WindowsServiceDescription service and can not find anything regarding issues with it or even how to start it.Inextirpable
I removed the full-stop, redeployed and tested it and it's still the exact same unfortunately.Inextirpable

© 2022 - 2024 — McMap. All rights reserved.