Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Easy way to get PerformContext

License

NotificationsYou must be signed in to change notification settings

meriturva/Hangfire.PerformContextAccessor

Repository files navigation

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 existingOWIN 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 theQuick start guide to learn about how to install Hangfire.

publicvoidConfiguration(IAppBuilderapp){// Register IPerformContextAccessor as transientapp.AddHangfirePerformContextAccessor();// Add Hangfire serviceapp.AddHangfire(config=>{// Add filter to handle PerformContextAccessorconfig.UsePerformContextAccessorFilter();});}

Use

A simple job that just injects a service and uses it.

usingSample.Service;publicclassSimpleJob{privateITestService_testService;publicSimpleJob(ITestServicetestService){_testService=testService;}publicasyncTaskDoJobAsync(){varjobId=await_testService.GetCurrentJobIdAsync();}}

A service that use IPerformContextAccessor

publicclassTestService:ITestService{privateIPerformContextAccessor_performContextAccessor;publicTestService(IPerformContextAccessorperformContextAccessor){_performContextAccessor=performContextAccessor;}publicasyncTask<string>GetCurrentJobIdAsync(){varjobId=_performContextAccessor.PerformingContext.BackgroundJob.Id;returnawaitTask.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}",

SeeNLog 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.

About

Easy way to get PerformContext

Resources

License

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp