日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hangfire源码解析-如何实现可扩展IOC的?

發(fā)布時間:2024/4/14 编程问答 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hangfire源码解析-如何实现可扩展IOC的? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、官方描述

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.

根據(jù)上述說明可以簡單理解為繼承“JobActivator”來實(shí)現(xiàn)自定義IOC容器。

二、JobActivator

//抽象Job生命周期 public abstract class JobActivatorScope : IDisposable {....省略//定義抽象方法,獲取實(shí)例public abstract object Resolve(Type type);//定義虛方法,摧毀生命周期public virtual void DisposeScope(){}}public class JobActivator {....省略//定義虛方法,默認(rèn)使用反射獲取實(shí)例public virtual object ActivateJob(Type jobType){return Activator.CreateInstance(jobType);}//定義虛方法,創(chuàng)建一個生命周期[Obsolete("Please implement/use the BeginScope(JobActivatorContext) method instead. Will be removed in 2.0.0.")]public virtual JobActivatorScope BeginScope(){return new SimpleJobActivatorScope(this);}//定義虛方法,創(chuàng)建一個生命周期public virtual JobActivatorScope BeginScope(JobActivatorContext context){ #pragma warning disable 618return BeginScope(); #pragma warning restore 618}//實(shí)現(xiàn)簡單的生命周期class SimpleJobActivatorScope : JobActivatorScope{private readonly JobActivator _activator;//存儲所有需要回收的實(shí)例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源碼實(shí)現(xiàn)

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 memberpublic 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是獲取或者創(chuàng)建,意味著實(shí)例沒有注入也會創(chuàng)建一個新的實(shí)例return ActivatorUtilities.GetServiceOrCreateInstance(_serviceScope.ServiceProvider, type);}public override void DisposeScope(){_serviceScope.Dispose();} }

四、Autofac 作為IOC容器

Hangfire.Autofac源碼實(shí)現(xiàn)

/// <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());} #endifclass AutofacScope : JobActivatorScope{private readonly ILifetimeScope _lifetimeScope;....省略//重寫public override object Resolve(Type type){return _lifetimeScope.Resolve(type);}//重寫public override void DisposeScope(){_lifetimeScope.Dispose();}} }

五、使用

在Hangfire源碼“CoreBackgroundJobPerformer”類中使用:

//執(zhí)行任務(wù) public object Perform(PerformContext context) {//創(chuàng)建一個生命周期using (var scope = _activator.BeginScope(new JobActivatorContext(context.Connection, context.BackgroundJob, context.CancellationToken))){object instance = null;....省略//任務(wù)是否為靜態(tài)方法,若是靜態(tài)方法需要從IOC容器中取出實(shí)例if (!context.BackgroundJob.Job.Method.IsStatic){instance = scope.Resolve(context.BackgroundJob.Job.Type);....省略}....省略} }

轉(zhuǎn)載于:https://www.cnblogs.com/yrinleung/p/10554322.html

總結(jié)

以上是生活随笔為你收集整理的Hangfire源码解析-如何实现可扩展IOC的?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。