Bonura.Hangfire.PerformContextAccessor
1.0.0
See the version list below for details.
dotnet add package Bonura.Hangfire.PerformContextAccessor --version 1.0.0
NuGet\Install-Package Bonura.Hangfire.PerformContextAccessor -Version 1.0.0
<PackageReference Include="Bonura.Hangfire.PerformContextAccessor" Version="1.0.0" />
paket add Bonura.Hangfire.PerformContextAccessor --version 1.0.0
#r "nuget: Bonura.Hangfire.PerformContextAccessor, 1.0.0"
// Install Bonura.Hangfire.PerformContextAccessor as a Cake Addin #addin nuget:?package=Bonura.Hangfire.PerformContextAccessor&version=1.0.0 // Install Bonura.Hangfire.PerformContextAccessor as a Cake Tool #tool nuget:?package=Bonura.Hangfire.PerformContextAccessor&version=1.0.0
Hangfire.PerformContextAccessor
Use PerformContextAccessor to access to PerformContext outside Job execution methods. It's only necessary to use IPerformContextAccessor when you need access to the job PerformContext inside a service.
Installation
Hangfire is available as a NuGet package. You can install it using the NuGet Package Console window:
PM> Install-Package Bonura.Hangfire.PerformContextAccessor
After installation, update your existing OWIN Startup file with the following lines of code. If you do not have this class in your project or don't know what is it, please read the Quick start guide to learn about how to install Hangfire.
public void Configuration(IAppBuilder app)
{
// Register IPerformContextAccessor as transient
app.AddHangfirePerformContextAccessor();
// Add Hangfire service
app.AddHangfire(config =>
{
// Add filter to handle PerformContextAccessor
config.UsePerformContextAccessorFilter();
});
}
Use
A simple job that just injects a service and uses it.
using Sample.Service;
public class SimpleJob
{
private ITestService _testService;
public SimpleJob(ITestService testService)
{
_testService = testService;
}
public async Task DoJobAsync()
{
var jobId = await _testService.GetCurrentJobIdAsync();
}
}
A service that use IPerformContextAccessor
public class TestService : ITestService
{
private IPerformContextAccessor _performContextAccessor;
public TestService(IPerformContextAccessor performContextAccessor)
{
_performContextAccessor = performContextAccessor;
}
public async Task<string> GetCurrentJobIdAsync()
{
var jobId = _performContextAccessor.PerformingContext.BackgroundJob.Id;
return await Task.FromResult(jobId);
}
}
Motivation
Access PerformContext (basically to get JobId) on a custom NLog layout (hangfire-jobid
):
"layout": "${longdate}|${level:uppercase=true}|${logger}|${message}|${hangfire-jobid}|${exception:format=toString}",
PS: NLog HangfireJobIdLayoutRenderer is coming soon on a different package
Remarks
As already defined for IHttpContextAccessor
- IPerformContextAccessor interface should be used with caution. As always, the PerformContext must not be captured outside of the Job execution flow.
- IPerformContextAccessor: Relies on System.Threading.AsyncLocal which can have a negative performance impact on asynchronous calls.
- Creates a dependency on "ambient state" which can make testing more difficult.
- IPerformContextAccessor.PerformContext may be null if accessed outside of the Job execution flow.
- To access information from PerformContext outside the Job execution flow, copy the information inside the execution flow. Be careful to copy the actual data and not just references. For example, rather than copying a reference to an IDictionary, copy the relevant header values or copy the entire dictionary key by key before leaving the flow.
- Don't capture IPerformContextAccessor.PerformContext in a constructor.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Hangfire.Core (>= 1.7.31)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Bonura.Hangfire.PerformContextAccessor:
Package | Downloads |
---|---|
BIA.Net.Core.WorkerService
WorkerService layer classes for BIA.Net Core Framework |
|
Hangfire.AspNetCore.Plus
Package Description |
|
Bonura.NLog.HangfireJobLogsTarget
Target for Hangfire.Dashboard.JobLogs |
|
Bonura.NLog.HangfireLayouts
Render layouts for Hangfire |
GitHub repositories
This package is not used by any popular GitHub repositories.