Hangfire源碼解析-如何實現可擴展IOC的?
阿新 • • 發佈:2019-03-18
若是 cor ets gin store pat acs exc lis
一、官方描述
These projects simplify the integration between Hangfire and your favorite IoC Container. They provide custom implementation of JobActivator class as well as registration extensions that allow you to use unit of work pattern or deterministic disposal in your background jobs.
根據上述說明可以簡單理解為繼承“JobActivator”來實現自定義IOC容器。
二、JobActivator
//抽象Job生命周期 public abstract class JobActivatorScope : IDisposable { ....省略 //定義抽象方法,獲取實例 public abstract object Resolve(Type type); //定義虛方法,摧毀生命周期 public virtual void DisposeScope() { } } public class JobActivator { ....省略 //定義虛方法,默認使用反射獲取實例 public virtual object ActivateJob(Type jobType) { return Activator.CreateInstance(jobType); } //定義虛方法,創建一個生命周期 [Obsolete("Please implement/use the BeginScope(JobActivatorContext) method instead. Will be removed in 2.0.0.")] public virtual JobActivatorScope BeginScope() { return new SimpleJobActivatorScope(this); } //定義虛方法,創建一個生命周期 public virtual JobActivatorScope BeginScope(JobActivatorContext context) { #pragma warning disable 618 return BeginScope(); #pragma warning restore 618 } //實現簡單的生命周期 class SimpleJobActivatorScope : JobActivatorScope { private readonly JobActivator _activator; //存儲所有需要回收的實例 private readonly List<IDisposable> _disposables = new List<IDisposable>(); public SimpleJobActivatorScope([NotNull] JobActivator activator) { if (activator == null) throw new ArgumentNullException(nameof(activator)); _activator = activator; } public override object Resolve(Type type) { var instance = _activator.ActivateJob(type); var disposable = instance as IDisposable; if (disposable != null) { _disposables.Add(disposable); } return instance; } public override void DisposeScope() { foreach (var disposable in _disposables) { disposable.Dispose(); } } } }
三、.Net Core 原生DI作為IOC容器
Hangfire.AspNetCore源碼實現
public class AspNetCoreJobActivator : JobActivator { private readonly IServiceScopeFactory _serviceScopeFactory; ....省略 public override JobActivatorScope BeginScope(JobActivatorContext context) { return new AspNetCoreJobActivatorScope(_serviceScopeFactory.CreateScope()); } #pragma warning disable CS0672 // Member overrides obsolete member public override JobActivatorScope BeginScope() #pragma warning restore CS0672 // Member overrides obsolete member { return new AspNetCoreJobActivatorScope(_serviceScopeFactory.CreateScope()); } } internal class AspNetCoreJobActivatorScope : JobActivatorScope { private readonly IServiceScope _serviceScope; ....省略 public override object Resolve(Type type) { //註意:AspNetCore是獲取或者創建,意味著實例沒有註入也會創建一個新的實例 return ActivatorUtilities.GetServiceOrCreateInstance(_serviceScope.ServiceProvider, type); } public override void DisposeScope() { _serviceScope.Dispose(); } }
四、Autofac 作為IOC容器
Hangfire.Autofac源碼實現
/// <summary>
/// Hangfire Job Activator based on Autofac IoC Container.
/// </summary>
public class AutofacJobActivator : JobActivator
{
/// <summary>
/// Tag used in setting up per-job lifetime scope registrations.
/// </summary>
public static readonly object LifetimeScopeTag = "BackgroundJobScope";
private readonly ILifetimeScope _lifetimeScope;
private readonly bool _useTaggedLifetimeScope;
....省略
//重寫
public override object ActivateJob(Type jobType)
{
return _lifetimeScope.Resolve(jobType);
}
#if NET45
//重寫
public override JobActivatorScope BeginScope()
{
return new AutofacScope(_useTaggedLifetimeScope
? _lifetimeScope.BeginLifetimeScope(LifetimeScopeTag)
: _lifetimeScope.BeginLifetimeScope());
}
#else
//重寫
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
return new AutofacScope(_useTaggedLifetimeScope
? _lifetimeScope.BeginLifetimeScope(LifetimeScopeTag)
: _lifetimeScope.BeginLifetimeScope());
}
#endif
class AutofacScope : JobActivatorScope
{
private readonly ILifetimeScope _lifetimeScope;
....省略
//重寫
public override object Resolve(Type type)
{
return _lifetimeScope.Resolve(type);
}
//重寫
public override void DisposeScope()
{
_lifetimeScope.Dispose();
}
}
}
五、使用
在Hangfire源碼“CoreBackgroundJobPerformer”類中使用:
//執行任務
public object Perform(PerformContext context)
{
//創建一個生命周期
using (var scope = _activator.BeginScope(
new JobActivatorContext(context.Connection, context.BackgroundJob, context.CancellationToken)))
{
object instance = null;
....省略
//任務是否為靜態方法,若是靜態方法需要從IOC容器中取出實例
if (!context.BackgroundJob.Job.Method.IsStatic)
{
instance = scope.Resolve(context.BackgroundJob.Job.Type);
....省略
}
....省略
}
}
Hangfire源碼解析-如何實現可擴展IOC的?