Skip to content

Commit

Permalink
Add support for custom scope configuration
Browse files Browse the repository at this point in the history
This allows configuring `ILifetimeScope` based on data available in the `JobActivatorContext`, such as job parameters.
  • Loading branch information
odinserj committed Sep 24, 2024
1 parent ac65834 commit 0646e23
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Hangfire.Autofac/AutofacJobActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class AutofacJobActivator : JobActivator
public static readonly object LifetimeScopeTag = "BackgroundJobScope";

private readonly ILifetimeScope _lifetimeScope;
#if !NET45
private readonly Action<ContainerBuilder, JobActivatorContext> _scopeConfigurationAction;
#endif
private readonly bool _useTaggedLifetimeScope;

/// <summary>
Expand All @@ -32,6 +35,18 @@ public AutofacJobActivator([NotNull] ILifetimeScope lifetimeScope, bool useTagge
_useTaggedLifetimeScope = useTaggedLifetimeScope;
}

#if !NET45
public AutofacJobActivator(
[NotNull] ILifetimeScope lifetimeScope,
[CanBeNull] Action<ContainerBuilder, JobActivatorContext> scopeConfigurationAction,
bool useTaggedLifetimeScope = true)
{
_lifetimeScope = lifetimeScope ?? throw new ArgumentNullException(nameof(lifetimeScope));
_scopeConfigurationAction = scopeConfigurationAction;
_useTaggedLifetimeScope = useTaggedLifetimeScope;
}
#endif

/// <inheritdoc />
public override object ActivateJob(Type jobType)
{
Expand All @@ -48,6 +63,18 @@ public override JobActivatorScope BeginScope()
#else
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
if (_scopeConfigurationAction != null)
{
Action<ContainerBuilder> configurationAction = builder =>
{
_scopeConfigurationAction(builder, context);
};

return new AutofacScope(_useTaggedLifetimeScope
? _lifetimeScope.BeginLifetimeScope(LifetimeScopeTag, configurationAction)
: _lifetimeScope.BeginLifetimeScope(configurationAction));
}

return new AutofacScope(_useTaggedLifetimeScope
? _lifetimeScope.BeginLifetimeScope(LifetimeScopeTag)
: _lifetimeScope.BeginLifetimeScope());
Expand Down
14 changes: 14 additions & 0 deletions src/Hangfire.Autofac/GlobalConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,19 @@ public static IGlobalConfiguration<AutofacJobActivator> UseAutofacActivator(

return configuration.UseActivator(new AutofacJobActivator(lifetimeScope, useTaggedLifetimeScope));
}

#if !NET45
public static IGlobalConfiguration<AutofacJobActivator> UseAutofacActivator(
[NotNull] this IGlobalConfiguration configuration,
[NotNull] ILifetimeScope lifetimeScope,
[CanBeNull] Action<ContainerBuilder, JobActivatorContext> scopeConfigurationAction,
bool useTaggedLifetimeScope = true)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (lifetimeScope == null) throw new ArgumentNullException(nameof(lifetimeScope));

return configuration.UseActivator(new AutofacJobActivator(lifetimeScope, scopeConfigurationAction, useTaggedLifetimeScope));
}
#endif
}
}

0 comments on commit 0646e23

Please sign in to comment.