- Notifications
You must be signed in to change notification settings - Fork1
Easy way to get PerformContext
License
meriturva/Hangfire.PerformContextAccessor
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
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();});}
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);}}
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
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.