Skip to content

Instantly share code, notes, and snippets.

@benhysell
Created October 23, 2015 18:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benhysell/bb58395da0373d1aab46 to your computer and use it in GitHub Desktop.
Save benhysell/bb58395da0373d1aab46 to your computer and use it in GitHub Desktop.
TopShelf - Create a Windows Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using V.RemoteDecodeProcessConsole.Properties;
namespace TopShelfExample
{
public class HangfireService
{
private BackgroundJobServer backgroundJobServer;
public HangfireService()
{
//Using Enterprise Library Logging
var configurationSource = ConfigurationSourceFactory.Create();
var logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
GlobalConfiguration.Configuration.UseRedisStorage(Settings.Default.RedisIpAndPort);
}
public void Start()
{
var options = new BackgroundJobServerOptions
{
Queues = new[] { "decodeccd" },
WorkerCount = 1
};
backgroundJobServer = new BackgroundJobServer(options);
}
public void Stop()
{
backgroundJobServer.Dispose();
}
}
}
//from a console application wrap it in TopShelf
//https://topshelf.readthedocs.org/en/latest/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Topshelf;
namespace TopShelfExample
{
class Program
{
static void Main()
{
HostFactory.Run(x =>
{
x.Service<HangfireService>(s =>
{
s.ConstructUsing(name => new HangfireService());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsPrompt();
x.StartAutomatically();
x.SetDescription("Remote decode Process Console");
x.SetDisplayName("RemoteDecodeFiles"); //note no spaces
x.SetServiceName("RemoteDecodeFiles"); //note no spaces
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment