-
-
Save karlosRivera/df23cde3e12bc752b964caab4757c8d7 to your computer and use it in GitHub Desktop.
TopShelf - Create a Windows Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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