日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

一、官方描述

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 618return 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 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是獲取或者創建,意味著實例沒有注入也會創建一個新的實例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());} #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”類中使用:

//執行任務 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);....省略}....省略} }

轉載于:https://www.cnblogs.com/yrinleung/p/10554322.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。