47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
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);
|
|
}
|
|
}
|