Skip to content

Instantly share code, notes, and snippets.

@logicbomb
Created February 14, 2012 22:00
Show Gist options
  • Save logicbomb/1830880 to your computer and use it in GitHub Desktop.
Save logicbomb/1830880 to your computer and use it in GitHub Desktop.
topshelf service install
using System;
using System.IO;
using Lemur.Logging;
using log4net.Config;
using NConfig;
using Topshelf;
namespace Lemur.CommandHost
{
public class Program : ILoggingSource
{
private Program()
{
}
public static void Main(string[] args)
{
NConfigurator.UsingFile(@"Config\custom.config").SetAsSystemDefault();
XmlConfigurator.Configure(new FileInfo(Path.Combine(Environment.CurrentDirectory, NConfigurator.Default.FileNames[0])));
var logSource = new LogSource();
// Report on configuration
logSource.Debug("Using configuration file: {0}", Path.Combine(Environment.CurrentDirectory, NConfigurator.Default.FileNames[0]));
logSource.Debug("Using connection string: {0}", System.Configuration.ConfigurationManager.ConnectionStrings["lemur"].ConnectionString);
try
{
HostFactory.Run(x =>
{
logSource.Debug("HostFactory is running");
x.Service<CommandHost>(s =>
{
try
{
logSource.Debug("HostConfigurator is running");
s.ConstructUsing(commandHost =>
{
logSource.Debug("Construct using new CommandHost()");
return new CommandHost();
});
logSource.Debug("ConstructUsing executed");
s.WhenStarted(commandHost =>
{
logSource.Debug("Calling commandHost.Start()");
commandHost.Start();
});
logSource.Debug("WhenStarted executed");
s.WhenStopped(commandHost =>
{
logSource.Debug("Calling commandHost.Stop()");
commandHost.Stop();
logSource.Debug("Calling commandHost.Stop()");
});
logSource.Debug("WhenStopped executed");
}
catch (Exception e)
{
logSource.Error("An error of type {0} occurred while running HostFactory.Service() command host {1}, {2}", e.GetType().Name, e.Message, e.StackTrace);
throw;
}
});
x.SetDescription("Processes commands issued by the Lemur Web Application");
x.SetDisplayName("Lemur Command Host");
x.SetServiceName("LemurCommandHost");
x.RunAsLocalSystem();
x.BeforeInstall(() => logSource.Debug("Installing Lemur Command Host Service"));
x.BeforeUninstall(() => logSource.Debug("UnInstalling Lemur Command Host Service"));
x.AfterInstall(() => logSource.Debug("Installed Lemur Command Host Service"));
x.AfterUninstall(() => logSource.Debug("Uninstalled Lemur Command Host Service"));
x.BeforeStartingServices(() => logSource.Debug("Starting command host"));
x.AfterStartingServices(() => logSource.Debug("Started command host"));
});
}
catch (Exception e)
{
logSource.Error(e.Message);
}
}
}
}
@logicbomb
Copy link
Author

Log output (including debug info from TopShelf)

My.CommandHost.LogSource [(null)] <(null)> - Using configuration file: ..
My.CommandHost.LogSource [(null)] <(null)> - Using connection string: ...
My.CommandHost.LogSource [(null)] <(null)> - HostFactory is running
My.CommandHost.LogSource [(null)] <(null)> - HostConfigurator is running
My.CommandHost.LogSource [(null)] <(null)> - ConstructUsing executed
My.CommandHost.LogSource [(null)] <(null)> - WhenStarted executed
My.CommandHost.LogSource [(null)] <(null)> - WhenStopped executed
Topshelf [(null)] <(null)> - Topshelf v2.2.2.0, .NET Framework v4.0.30319.239
Topshelf [(null)] <(null)> - Topshelf v2.2.2.0, .NET Framework v4.0.30319.239
Topshelf.Hosts.InstallHost [(null)] <(null)> - Attempting to install 'MyCommandHost'
Topshelf.Hosts.InstallHost [(null)] <(null)> - Attempting to install 'MyCommandHost'
My.CommandHost.LogSource [(null)] <(null)> - Installing My Command Host Service
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Installing My Command Host service
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Installing My Command Host service
My.CommandHost.LogSource [(null)] <(null)> - Installing My Command Host Service
My.CommandHost.LogSource [(null)] <(null)> - Installed My Command Host Service
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Opening Registry
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Opening Registry
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Service path: "d:\Projects\My Technologies\deploy\dev\command-host\My.CommandHost.exe"
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Service path: "d:\Projects\My Technologies\deploy\dev\command-host\My.CommandHost.exe"
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Image path: "d:\Projects\My Technologies\deploy\dev\command-host\My.CommandHost.exe" -displayname "My Command Host" -servicename:MyCommandHost
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Image path: "d:\Projects\My Technologies\deploy\dev\command-host\My.CommandHost.exe" -displayname "My Command Host" -servicename:MyCommandHost
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Closing Registry
Topshelf.WindowsServiceCode.HostInstaller [(null)] <(null)> - Closing Registry
My.CommandHost.LogSource [(null)] <(null)> - Installed My Command Host Service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment