TOPSHELF - AN EASY SERVICE HOSTING FRAMEWORK FOR BUILDING WINDOWS SERVICES USING .NET Larry Nung
AGENDA Introduction Getting started Custom Service Command-Line Reference Q & A 2
INTRODUCTION 3
INTRODUCTION  Put Your Apps on the Topshelf  An easy service hosting framework for building Windows services using .NET 4
GETTING STARTED 5
GETTING STARTED 6
GETTING STARTED 7
GETTING STARTED using System; using Topshelf; … class Program { static void Main(string[] args) { HostFactory.Run(x => { x.Service<Program>(s => { s.ConstructUsing(name => new Program()); s.WhenStarted(p => p.Start()); s.WhenStopped(p => p.Stop()); }); }); } public void Start() { Console.WriteLine("Service start..."); } public void Stop() { Console.WriteLine("Service stop..."); } } 8
GETTING STARTED 9
CUSTOM SERVICE 10
CUSTOM SERVICE using System; using Topshelf; ... class Program { static void Main(string[] args) { HostFactory.Run(x => { x.Service<ProgramService>(); }); } } class ProgramService : ServiceControl { public bool Start(HostControl hostControl) { Console.WriteLine("Service start..."); return true; } public bool Stop(HostControl hostControl) { Console.WriteLine("Service stop..."); return true; } } ... 11
COMMAND-LINE 12
COMMAND-LINE service.exe [verb] [-option:value] [-switch] run Runs the service from the command line (default) help, --help Displays help install Installs the service --autostart The service should start automatically (default) --disabled The service should be set to disabled --manual The service should be started manually --delayed The service should start automatically (delayed) -instance An instance name if registering the service multiple times -username The username to run the service -password The password for the specified username --localsystem Run the service with the local system account --localservice Run the service with the local service account --networkservice Run the service with the network service permission --interactive The service will prompt the user at installation for the service credentials 13
COMMAND-LINE start Start the service after it has been installed --sudo Prompts for UAC if running on Vista/W7/2008 -servicename The name that the service should use when installing -description The service description the service should use when installing -displayname The display name the the service should use when installing start Starts the service if it is not already running stop Stops the service if it is running uninstall Uninstalls the service -instance An instance name if registering the service multiple times --sudo Prompts for UAC if running on Vista/W7/2008 14
COMMAND-LINE Examples: service install Installs the service into the service control manager service install -username:joe -password:bob --autostart Installs the service using the specified username/password and configures the service to start automatically at machine startup service uninstall Uninstalls the service service install -instance:001 Installs the service, appending the instance name to the service name so that the service can be installed multiple times. You may need to tweak the log4net.config to make this play nicely with the log files. 15
COMMAND-LINE 16
COMMAND-LINE 17
COMMAND-LINE 18
COMMAND-LINE 19
COMMAND-LINE 20
COMMAND-LINE 21
REFERENCE 22
REFERENCE  Topshelf  http://topshelf-project.com/  Welcome to Topshelf’s documentation! — Topshelf 3.0 documentation  https://topshelf.readthedocs.io/en/latest/index.html  Topshelf/Topshelf: An easy service hosting framework for building Windows services using .NET  https://github.com/Topshelf/Topshelf 23
Q&A 24
QUESTION & ANSWER 25

Topshelf - An easy service hosting framework for building Windows services using .NET

  • 1.
    TOPSHELF - ANEASY SERVICE HOSTING FRAMEWORK FOR BUILDING WINDOWS SERVICES USING .NET Larry Nung
  • 2.
  • 3.
  • 4.
    INTRODUCTION  Put YourApps on the Topshelf  An easy service hosting framework for building Windows services using .NET 4
  • 5.
  • 6.
  • 7.
  • 8.
    GETTING STARTED using System; usingTopshelf; … class Program { static void Main(string[] args) { HostFactory.Run(x => { x.Service<Program>(s => { s.ConstructUsing(name => new Program()); s.WhenStarted(p => p.Start()); s.WhenStopped(p => p.Stop()); }); }); } public void Start() { Console.WriteLine("Service start..."); } public void Stop() { Console.WriteLine("Service stop..."); } } 8
  • 9.
  • 10.
  • 11.
    CUSTOM SERVICE using System; usingTopshelf; ... class Program { static void Main(string[] args) { HostFactory.Run(x => { x.Service<ProgramService>(); }); } } class ProgramService : ServiceControl { public bool Start(HostControl hostControl) { Console.WriteLine("Service start..."); return true; } public bool Stop(HostControl hostControl) { Console.WriteLine("Service stop..."); return true; } } ... 11
  • 12.
  • 13.
    COMMAND-LINE service.exe [verb] [-option:value][-switch] run Runs the service from the command line (default) help, --help Displays help install Installs the service --autostart The service should start automatically (default) --disabled The service should be set to disabled --manual The service should be started manually --delayed The service should start automatically (delayed) -instance An instance name if registering the service multiple times -username The username to run the service -password The password for the specified username --localsystem Run the service with the local system account --localservice Run the service with the local service account --networkservice Run the service with the network service permission --interactive The service will prompt the user at installation for the service credentials 13
  • 14.
    COMMAND-LINE start Start theservice after it has been installed --sudo Prompts for UAC if running on Vista/W7/2008 -servicename The name that the service should use when installing -description The service description the service should use when installing -displayname The display name the the service should use when installing start Starts the service if it is not already running stop Stops the service if it is running uninstall Uninstalls the service -instance An instance name if registering the service multiple times --sudo Prompts for UAC if running on Vista/W7/2008 14
  • 15.
    COMMAND-LINE Examples: service install Installs theservice into the service control manager service install -username:joe -password:bob --autostart Installs the service using the specified username/password and configures the service to start automatically at machine startup service uninstall Uninstalls the service service install -instance:001 Installs the service, appending the instance name to the service name so that the service can be installed multiple times. You may need to tweak the log4net.config to make this play nicely with the log files. 15
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
    REFERENCE  Topshelf  http://topshelf-project.com/ Welcome to Topshelf’s documentation! — Topshelf 3.0 documentation  https://topshelf.readthedocs.io/en/latest/index.html  Topshelf/Topshelf: An easy service hosting framework for building Windows services using .NET  https://github.com/Topshelf/Topshelf 23
  • 24.
  • 25.