Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time

Show me the code!

All right, all right, already. Here you go. Below is a functional setup of Topshelf.

Now for the play by play.

  1. Here we are setting up the host using the HostFactory.Run the runner. We open up a new lambda where the x in this case exposes all of the host level configuration. Using this approach the command arguments are extracted from environment variables. We also capture the return code of the service - which we return on line 11.
  2. Here we are telling Topshelf that there is a service of type TownCrier. The lambda that gets opened here is exposing the service configuration options through the s parameter.
  3. This tells Topshelf how to build an instance of the service. Currently we are just going to ‘new it up’ but we could just as easily pull it from an IoC container with some code that would look something like container.GetInstance<TownCrier>()
  4. How does Topshelf start the service
  5. How does Topshelf stop the service
  6. Here we are setting up the ‘run as’ and have selected the ‘local system’. We can also set up from the command line interactively with a win from type prompt and we can also just pass in some username/password as string arguments
  7. Here we are setting up the description for the winservice to be use in the windows service monitor
  8. Here we are setting up the display name for the winservice to be use in the windows service monitor
  9. Here we are setting up the service name for the winservice to be use in the windows service monitor
  10. Now that the lambda has closed, the configuration will be executed and the host will start running.
  11. Finally, we convert and return the service exit code.


You can only have ONE service! As of 3.x Topshelf the base product no longer support hosting multiple services. This was done because the code to implement was very brittle and hard to debug. We have opted for a simpler and cleaner base product. This feature will most likely come back in the form of an add on nuget.