This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,47 @@
using System;
using Hangfire;
using SimpleInjector;
namespace Kreta.Web.HangfireJobActivator
{
public class ContainerJobActivator : JobActivator
{
private readonly Container container;
public ContainerJobActivator(Container container)
{
this.container = container ?? throw new ArgumentNullException(nameof(container));
}
public override object ActivateJob(Type type) => this.container.GetInstance(type);
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
return new InjectorScope(this.container);
}
}
internal class InjectorScope : JobActivatorScope
{
private readonly Container container;
public InjectorScope(Container container)
{
this.container = container;
SimpleInjector.Lifestyles.AsyncScopedLifestyle.BeginScope(this.container);
}
public override void DisposeScope()
{
Scope currentExecutionContextScope = Lifestyle.Scoped.GetCurrentScope(this.container);
if (currentExecutionContextScope != null)
{
currentExecutionContextScope.Dispose();
}
}
public override object Resolve(Type type) =>
this.container.GetInstance(type);
}
}