Hangfire源码解析-如何实现可扩展IOC的?
程序员文章站
2022-07-04 19:26:47
一、 "官方描述" These projects simplify the integration between Hangfire and your favorite IoC Container. They provide custom implementation of JobActivator ......
一、
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容器
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); ....省略 } ....省略 } }
推荐阅读