-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
add hangfire ContainerJobActivator
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Hangfire; | ||
using NetCorePal.Extensions.Hangfire; | ||
|
||
namespace Microsoft.AspNetCore.Builder; | ||
|
||
public static class ApplicationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// 使用DependencyInjection容器作为JobActivator | ||
/// </summary> | ||
/// <param name="app"></param> | ||
/// <returns></returns> | ||
public static IApplicationBuilder UseHangfireContainerJobActivator(this IApplicationBuilder app) | ||
{ | ||
var jobActivator = new ContainerJobActivator(app.ApplicationServices); | ||
GlobalConfiguration.Configuration.UseActivator(jobActivator); | ||
return app; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Hangfire; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace NetCorePal.Extensions.Hangfire; | ||
|
||
public class ContainerJobActivator : JobActivator | ||
{ | ||
#pragma warning disable S2933 | ||
private IServiceProvider _container; | ||
|
||
/// <summary> | ||
/// 容器job激活 | ||
/// </summary> | ||
public ContainerJobActivator(IServiceProvider container) | ||
{ | ||
_container = container; | ||
} | ||
|
||
/// <summary> | ||
/// 开始范围 | ||
/// </summary> | ||
public override JobActivatorScope BeginScope() | ||
Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs
|
||
{ | ||
return new ServiceScopeJobActivatorScope(_container.CreateScope()); | ||
} | ||
|
||
/// <summary> | ||
/// 激活job | ||
/// </summary> | ||
public override object ActivateJob(Type jobType) | ||
{ | ||
return _container.GetRequiredService(jobType); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" /> | ||
<PackageReference Include="Hangfire.Core" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Hangfire; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace NetCorePal.Extensions.Hangfire; | ||
|
||
public class ServiceScopeJobActivatorScope(IServiceScope scope) : JobActivatorScope | ||
{ | ||
/// <summary> | ||
/// 处理范围 | ||
/// </summary> | ||
public override void DisposeScope() | ||
{ | ||
scope.Dispose(); | ||
} | ||
|
||
/// <summary> | ||
/// 获取服务 | ||
/// </summary> | ||
public override object Resolve(Type type) | ||
{ | ||
return scope.ServiceProvider.GetRequiredService(type); | ||
} | ||
} |