Skip to content

Commit

Permalink
Merge pull request #30 from netcorepal/dev
Browse files Browse the repository at this point in the history
add hangfire ContainerJobActivator
  • Loading branch information
witskeeper committed May 12, 2024
2 parents 9fb5cd5 + 1f0288c commit e0948ce
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<FrameworkVersion>6.0.0</FrameworkVersion>
<ExtensionsVersion>6.0.0</ExtensionsVersion>
Expand Down Expand Up @@ -60,6 +63,7 @@
<PackageReference Update="DotNetCore.CAP.PostgreSql" Version="7.2.0" />
<PackageReference Update="DotNetCore.CAP.RabbitMQ" Version="7.2.0" />
<PackageReference Update="DotNetCore.CAP.Dashboard" Version="7.2.0" />
<PackageReference Update="Hangfire.Core" Version="1.8.0" />
<PackageReference Update="MediatR" Version="12.1.1" />
<PackageReference Update="DistributedLock.Redis" Version="1.0.2" />
<PackageReference Update="System.Reactive" Version="6.0.0" />
Expand Down
7 changes: 7 additions & 0 deletions netcorepal-cloud-framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Snowf
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Snowflake.Redis.UnitTests", "test\NetCorePal.Extensions.Snowflake.Redis.UnitTests\NetCorePal.Extensions.Snowflake.Redis.UnitTests.csproj", "{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCorePal.Extensions.Hangfire", "src\Hangfire\NetCorePal.Extensions.Hangfire.csproj", "{664491B2-2001-4D42-8029-4EF4B1CDBEE6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -329,6 +331,10 @@ Global
{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB}.Release|Any CPU.Build.0 = Release|Any CPU
{664491B2-2001-4D42-8029-4EF4B1CDBEE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{664491B2-2001-4D42-8029-4EF4B1CDBEE6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{664491B2-2001-4D42-8029-4EF4B1CDBEE6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{664491B2-2001-4D42-8029-4EF4B1CDBEE6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -387,6 +393,7 @@ Global
{EF1C82DE-D48B-4FD3-BF02-7469E9E5A0E2} = {605436C1-2560-47BA-99D4-7A80B628230F}
{FA85B2AD-3891-4F9A-ACB5-545E1DC07519} = {518B90D3-B430-464C-9BE1-4FC83365B94B}
{ED4401A4-0B4B-4FF5-AEF3-55D5700A13FB} = {DB5A8331-2B57-4F2B-8F7D-02E167BCD8BB}
{664491B2-2001-4D42-8029-4EF4B1CDBEE6} = {605436C1-2560-47BA-99D4-7A80B628230F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D46201E5-1E3C-47A0-8584-4ACCC135CF9A}
Expand Down
19 changes: 19 additions & 0 deletions src/Hangfire/ApplicationBuilderExtensions.cs
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;
}
}
34 changes: 34 additions & 0 deletions src/Hangfire/ContainerJobActivator.cs
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

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.

Check warning on line 22 in src/Hangfire/ContainerJobActivator.cs

View workflow job for this annotation

GitHub Actions / build

Member 'ContainerJobActivator.BeginScope()' overrides obsolete member 'JobActivator.BeginScope()'. Add the Obsolete attribute to 'ContainerJobActivator.BeginScope()'.
{
return new ServiceScopeJobActivatorScope(_container.CreateScope());
}

/// <summary>
/// 激活job
/// </summary>
public override object ActivateJob(Type jobType)
{
return _container.GetRequiredService(jobType);
}
}
15 changes: 15 additions & 0 deletions src/Hangfire/NetCorePal.Extensions.Hangfire.csproj
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>
23 changes: 23 additions & 0 deletions src/Hangfire/ServiceScopeJobActivatorScope.cs
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);
}
}

0 comments on commit e0948ce

Please sign in to comment.