Bonura.Hangfire.PerformContextAccessor 1.3.3

dotnet add package Bonura.Hangfire.PerformContextAccessor --version 1.3.3
NuGet\Install-Package Bonura.Hangfire.PerformContextAccessor -Version 1.3.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bonura.Hangfire.PerformContextAccessor" Version="1.3.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bonura.Hangfire.PerformContextAccessor --version 1.3.3
#r "nuget: Bonura.Hangfire.PerformContextAccessor, 1.3.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Bonura.Hangfire.PerformContextAccessor as a Cake Addin
#addin nuget:?package=Bonura.Hangfire.PerformContextAccessor&version=1.3.3

// Install Bonura.Hangfire.PerformContextAccessor as a Cake Tool
#tool nuget:?package=Bonura.Hangfire.PerformContextAccessor&version=1.3.3

Hangfire.PerformContextAccessor

Latest version

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.PerformContextAccessor 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}",

See NLog Hangfire Layouts: https://github.com/meriturva/NLog.HangfireLayouts

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 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 is compatible. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Bonura.Hangfire.PerformContextAccessor:

Package Downloads
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.

Version Downloads Last updated
1.3.3 6,623 2/22/2024
1.3.2 11,115 9/27/2023
1.3.1 124 9/27/2023
1.3.0 378 8/24/2023
1.3.0-beta 132 7/31/2023
1.2.0 12,621 1/2/2023
1.1.0 3,299 10/2/2022
1.0.0 385 9/30/2022