Skip to content

Instantly share code, notes, and snippets.

@domagojmedo
Created June 1, 2022 06:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domagojmedo/9eb783938d2902ade1e45295b76dc85b to your computer and use it in GitHub Desktop.
Save domagojmedo/9eb783938d2902ade1e45295b76dc85b to your computer and use it in GitHub Desktop.
Hangfire constructor injection of JobActivatorContext
using Hangfire;
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.AddScoped<IUser, User>();
services.AddScoped<IHangfireContext, HangfireContext>();
services.AddTransient<CustomJobActivator>();
services.AddHangfire((provider, configuration) =>
{
configuration.UseActivator(provider.GetService<CustomJobActivator>()!);
configuration.UseSqlServerStorage("server=.;database=Hangfire;trusted_connection=True;");
});
services.AddHangfireServer();
})
.Build();
var background = host.Services.GetService<IBackgroundJobClient>();
background.Enqueue<TestJob>(x => x.Run());
await host.RunAsync();
public class TestJob
{
private readonly ILogger<TestJob> _logger;
private readonly IUser _user;
public TestJob(ILogger<TestJob> logger, IUser user)
{
_logger = logger;
_user = user;
}
public void Run()
{
_logger.LogInformation("Job run for id {job} job name {name}", _user.Id, _user.Name);
}
}
public interface IUser
{
public string Id { get; }
public string Name { get; }
}
public class User : IUser
{
public User(IHangfireContext hangfireContext)
{
Id = hangfireContext.JobActivatorContext?.BackgroundJob.Id ?? "-";
Name = hangfireContext.JobActivatorContext?.BackgroundJob.Job.Type.Name ?? "-";
}
public string Id { get; }
public string Name { get; }
}
public class CustomJobActivator : JobActivator
{
private readonly IServiceProvider _serviceProvider;
public CustomJobActivator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public override object ActivateJob(Type jobType)
{
return _serviceProvider.GetService(jobType)!;
}
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
return new CustomScope(_serviceProvider.CreateScope(), context);
}
}
public class CustomScope : JobActivatorScope
{
private readonly IServiceScope _serviceScope;
public CustomScope(IServiceScope serviceScope, JobActivatorContext context)
{
_serviceScope = serviceScope;
var hangfireContext = _serviceScope.ServiceProvider.GetRequiredService<IHangfireContext>();
hangfireContext.JobActivatorContext = context;
}
public override object Resolve(Type type)
{
return ActivatorUtilities.CreateInstance(_serviceScope.ServiceProvider, type);
}
public override void DisposeScope()
{
_serviceScope.Dispose();
}
}
public class HangfireContext : IHangfireContext
{
public JobActivatorContext? JobActivatorContext { get; set; }
}
public interface IHangfireContext
{
JobActivatorContext? JobActivatorContext { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment