Is there a way of starting Hangfire background jobs from ServiceStack services? I've already been able to start jobs from within MVC where I can resolve ServiceStack services but I wanted to be able to do this from within ServiceStack.
After some more investigation I came across this post setup example without owin?.
One workaround would be to not install the whole Hangfire nuget package but just Hangfire.Core and Hangfire.SqlServer (or respective storage option), which requires only the reference of Owin package. The only drawback is that you cannot use Hangfire Dashboard.
Then start Hangfire and any job through the following code:
JobStorage.Current = new SqlServerStorage("connection string");
var server = new BackgroundJobServer();
server.Start();
RecurringJob.AddOrUpdate(() => System.Diagnostics.Debug.WriteLine("No OWIN"), Cron.Minutely);
In addition you could use the ServiceStack Funq for the HangFire JobActivator:
JobActivator.Current = new FunqJobActivator(container);
after creating FunqJobActivator as follows:
public override object ActivateJob(Type jobType)
{
var resolved = _container.TryResolve(jobType);
if (resolved == null)
{
foreach (Type it in jobType.GetInterfaces())
{
resolved = _container.TryResolve(it);
if(resolved != null)break;
}
}
return resolved;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With