Introduction

So what is TopShelf

According to the site.

One of the simplest ways to get started with winservice development

In other words it’s an easier way to make a windows service.

Getting started

To get started you create a consoleapplication and add topshelf via nuget (this is 2012 after all).

Then we create a class with a stop and start method to begin with.

Public Class ServiceClass

        Public Sub StartService()
            WriteToEventLog("Service started")
        End Sub
        Public Sub StopService()
            WriteToEventLog("Service stopped")
        End Sub

        Private Sub WriteToEventLog(ByVal Message As String)
            Dim el As New EventLog("Application")
            el.Source = "VSS"
            Try
                el.WriteEntry(Message, EventLogEntryType.Information)
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
        End Sub
    End Class```
This writes something to the eventlog using the VSS eventsource, because that was there and I was to lazy to create my own source.

I called my methods StartService and StopService because Stop is a reserved word in VB.

And then we have to configure our service.

```vbnet
Imports Topshelf

Module Module1

    Sub Main()
        HostFactory.Run(
            Sub(x)
                x.Service(Of ServiceClass)(
                    Sub(s)
                        s.ConstructUsing(Function(name) New ServiceClass())
                        s.WhenStarted(Sub(tc) tc.StartService())
                        s.WhenStopped(Sub(tc) tc.StopService())
                    End Sub)
                x.RunAsLocalSystem()
                x.SetDescription("Sample Topshelf Host With VB")
                x.SetDisplayName("Displayname of VBService")
                x.SetServiceName("VBService")
            End Sub)
    End Sub
End Module

If we now got to our eventlog there will be an entry for the VSS source there that has this in the body.

We can now run this.

In the evntlog we will now see this.

And if we exit with Ctrl+C we will see this.

Conclusion

So it works and is fairly easy to setup and make it work.