First time using hangfire with .net core here. I have couple of questions regarding setting as background service and secondly multiple projects calling single hangfire integration. So here is my basic setup.
I have 2 projects: One BacgroundService & Another Background Engine
Under BacgroundService Project I have my Program.cs class :
namespace My.BackgroundService
{
public class Program
{
private const string RunAsServiceFlag = "service";
public static void Main(string[] args)
{
try
{
if (args.Contains(RunAsServiceFlag))
{
RunAsService(args);
}
else
{
RunInteractive(args);
}
}
catch (Exception ex)
{
Console.WriteLine($"An error ocurred: {ex.Message}");
}
}
private static void RunAsService(string[] args)
{
var service = new ApplicationService(args.Where(a => a != RunAsServiceFlag).ToArray());
var serviceHost = new Win32ServiceHost(service);
serviceHost.Run();
}
private static void RunInteractive(string[] args)
{
var service = new ApplicationService(args);
service.Start(new string[0], () => { });
Console.WriteLine("Running service, press enter to stop.");
Console.ReadLine();
service.Stop();
}
}
}
Below is my startup class:
namespace My.BackgroundService
{
public class Startup
{
private IConfigurationRoot _configuration;
public Startup(IHostingEnvironment env)
{
_configuration = new ConfigurationBuilder()
.AddXmlFile("ConnectionStrings.config")
.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
//This is a part of my JobEngine project
services.AddTransient <ITest, Test>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseHangfireServer(new BackgroundJobServerOptions { WorkerCount = 2 });
app.UseHangfireDashboard();
}
}
}
I have my ApplicationService as:
namespace My.BackgroundService
{
internal class ApplicationService : IWin32Service
{
private const string Endpoint = "http://localhost:12345";
private readonly string[] commandLineArguments;
private IWebHost webHost;
private bool stopRequestedByWindows;
public ApplicationService(string[] commandLineArguments)
{
this.commandLineArguments = commandLineArguments;
}
public string ServiceName => "My Background Service";
public void Start(string[] startupArguments, ServiceStoppedCallback serviceStoppedCallback)
{
// in addition to the arguments that the service has been registered with,
// each service start may add additional startup parameters.
// To test this: Open services console, open service details, enter startup arguments and press start.
string[] combinedArguments;
if (startupArguments.Length > 0)
{
combinedArguments = new string[commandLineArguments.Length + startupArguments.Length];
Array.Copy(commandLineArguments, combinedArguments, commandLineArguments.Length);
Array.Copy(startupArguments, 0, combinedArguments, commandLineArguments.Length, startupArguments.Length);
}
else
{
combinedArguments = commandLineArguments;
}
var config = new ConfigurationBuilder()
.AddCommandLine(combinedArguments)
.Build();
webHost = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.UseUrls(Endpoint)
.UseConfiguration(config)
.Build();
// Make sure the windows service is stopped if the
// ASP.NET Core stack stops for any reason
webHost
.Services
.GetRequiredService<IApplicationLifetime>()
.ApplicationStopped
.Register(() =>
{
if (stopRequestedByWindows == false)
{
serviceStoppedCallback();
}
});
webHost.Start();
}
public void Stop()
{
stopRequestedByWindows = true;
webHost.Dispose();
}
}
}
Above is how I setup my hangfire. I have a project called JobEngine where I want to configure all my services (There are 4-5 background services that we want to setup).
In this project I have a test service as:
namespace My.JobEngine
{
public interface ITest
{
void RunBackgroundJob(string jobId, string JsonArgs);
}
}
namespace AEBGCommon.JobEngine
{
public class Test: ITest
{
public void RunBackgroundJob(string jobId, string jsonArgs)
{
try
{
//custom code here
}
catch (Exception ex)
{
}
}
}
}
Now I want to enque and schedule the above service so that it runs on background. Where would I make call to the above and setup enque. Also I would be having muliple services like this which I want to run in the background at particular times. I am not sure how can we enque in .net core.
Sorry for long post. Would appreciate inputs.