{"id":25002633,"url":"https://github.com/skwasjer/hangfire.correlate","last_synced_at":"2025-04-09T16:18:29.787Z","repository":{"id":35030654,"uuid":"198477215","full_name":"skwasjer/Hangfire.Correlate","owner":"skwasjer","description":"Hangfire integration of Correlate to add correlation id support to Hangfire background/scheduled jobs.","archived":false,"fork":false,"pushed_at":"2025-02-17T22:21:55.000Z","size":130,"stargazers_count":17,"open_issues_count":5,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T16:18:21.066Z","etag":null,"topics":["c-sharp","correlate","correlation","correlation-context","correlation-id","csharp","dotnet","dotnet-core","hangfire"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skwasjer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-07-23T17:22:34.000Z","updated_at":"2025-03-10T06:54:54.000Z","dependencies_parsed_at":"2023-11-18T02:00:02.054Z","dependency_job_id":"d517e2b6-7ced-47e1-8cd9-084b06e286c3","html_url":"https://github.com/skwasjer/Hangfire.Correlate","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skwasjer%2FHangfire.Correlate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skwasjer%2FHangfire.Correlate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skwasjer%2FHangfire.Correlate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skwasjer%2FHangfire.Correlate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skwasjer","download_url":"https://codeload.github.com/skwasjer/Hangfire.Correlate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065281,"owners_count":21041872,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-sharp","correlate","correlation","correlation-context","correlation-id","csharp","dotnet","dotnet-core","hangfire"],"created_at":"2025-02-04T21:53:13.335Z","updated_at":"2025-04-09T16:18:29.767Z","avatar_url":"https://github.com/skwasjer.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hangfire.Correlate\n\n[Hangfire](https://www.hangfire.io/) integration of [Correlate](https://github.com/skwasjer/Correlate) to add correlation id support to Hangfire background/scheduled jobs.\n\n## Installation\n\nInstall Hangfire.Correlate via the Nuget package manager or `dotnet` cli.\n\n```powershell\ndotnet add package Hangfire.Correlate\n```\n\n---\n\n[![Main workflow](https://github.com/skwasjer/Hangfire.Correlate/actions/workflows/main.yml/badge.svg)](https://github.com/skwasjer/Hangfire.Correlate/actions/workflows/main.yml)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=skwasjer_Hangfire.Correlate\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=skwasjer_Hangfire.Correlate)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=skwasjer_Hangfire.Correlate\u0026metric=coverage)](https://sonarcloud.io/component_measures?id=skwasjer_Hangfire.Correlate\u0026metric=coverage)\n\n\n| | | |\n|---|---|---|\n| `Hangfire.Correlate` | [![NuGet](https://img.shields.io/nuget/v/Hangfire.Correlate.svg)](https://www.nuget.org/packages/Hangfire.Correlate/) [![NuGet](https://img.shields.io/nuget/dt/Hangfire.Correlate.svg)](https://www.nuget.org/packages/Hangfire.Correlate/) | Correlate integration with Hangfire. |\n\n## Correlation ID flow\n\nThe Correlate framework provides an ambient correlation context scope, that makes it easy to track a Correlation ID passing through (micro)services.\n\nThis library specifically provides a job filter for [Hangfire](https://www.hangfire.io/) and ensures each job is performed (run) in its own correlation context provided by [Correlate](https://github.com/skwasjer/Correlate).\n\n### Job creation\n\nWhenever a job is enqueued in an existing correlation context, the current correlation id will be attached to the job as a job parameter. If no correlation context is available when the job is enqueued, no parameter is added.\n\n### Job execution\n\nWhen the job is 'performed' (in Hangfire terms), the job parameter (for correlation id) that was added during job creation will be used to create a new correlation context, thus reviving the correlation context. This means that even in distributed scenarios, the same correlation id is used to process the job.\n\u003e If no correlation id was stored with the job, yet also to remain backwards compatible with existing jobs (prior to Correlate integration), the job id will be used instead. \n\n### Job continuation\n\nA continuation job will inherit the correlation id from the parent job, unless explicitly inside an active correlation context.\n\n## Usage ###\n\nConfigure Hangfire to use Correlate.\n\n### Using a `IServiceProvider`\n\nUse the configuration extension with `IServiceProvider` to configure Hangfire with Correlate.\n\nAdd package dependencies:\n- [Correlate.DependencyInjection](https://github.com/skwasjer/Correlate)\n\n```csharp\nservices\n    .AddLogging(logging =\u003e logging.AddConsole())\n    .AddCorrelate()\n    .AddHangfire((serviceProvider, config) =\u003e config\n        .UseCorrelate(serviceProvider)\n        .(...)\n    );\n```\n\n### Using built-in configuration extensions ###\n\nUse the Hangfire built-in configuration extensions to enable Correlate.\n\n\u003e This is no longer the recommended approach and will most likely be removed in a future version.\n\n```csharp\nILoggerFactory loggerFactory = new LoggerFactory();\nloggerFactory.AddConsole();\n\nGlobalConfiguration.Configuration\n    .UseCorrelate(loggerFactory)\n    .(...);\n```\n\n## Enqueue jobs\n\nThis example illustrates how jobs that are enqueued, inherit the Correlation ID from the ambient correlation context if inside one.\n\n```csharp\npublic class MyService\n{\n    private IAsyncCorrelationManager _correlationManager;\n    private IBackgroundJobClient _client;\n\n    public MyService(IAsyncCorrelationManager correlationManager, IBackgroundJobClient client)\n    {\n        _correlationManager = _correlationManager;\n        _client = client;\n    }\n\n    public async Task DoWork()\n    {\n        // Without ambient correlation context, the job id will be used.\n        _client.Enqueue(() =\u003e Thread.Sleep(1000));\n\n        // Perform work in new correlation context.\n        string parentJobId = await _correlationManager.CorrelateAsync(async () =\u003e\n        {\n            // This job be executed with the Correlation ID from\n            // the ambient correlation context which is automatically\n            // generated.\n            _client.Enqueue(() =\u003e Thread.Sleep(1000));\n\n            // Do stuff.\n            await ..\n\n            // This job will be also be executed with the same Correlation ID.\n            return _client.Enqueue\u003cMyJob\u003e(myJob =\u003e myJob.CallApiAsync());\n        });\n\n          // This job will be also be executed with the same Correlation ID\n          // as which MyJob.CallApiAsync() was executed, even though it is\n          // outside the ambient correlation context, because it is a \n          // continuation and we used its job id to enqueue it.\n        _client.ContinueJobWith(parentJobId, () =\u003e Thread.Sleep(1000));\n    }\n}\n```\n\n\u003e Note: when using Correlate integration for ASP.NET Core, each request is already scoped to a correlation context, and so there is no need to wrap the enqueueing of jobs with `IAsyncCorrelationManager`/`ICorrelationManager`.\n\n## More info\n\nSee [Correlate](https://github.com/skwasjer/Correlate) documentation for further integration with ASP.NET Core, `IHttpClientFactory` and for other extensions/libraries.\n\n### Supported .NET targets\n- .NET Standard 2.0\n\n### Build requirements\n- Visual Studio 2022\n- .NET 6.0\n- .NET Core 3.1\n\n#### Contributions\nPR's are welcome. Please rebase before submitting, provide test coverage, and ensure the AppVeyor build passes. I will not consider PR's otherwise.\n\n#### Contributors\n- skwas (author/maintainer)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskwasjer%2Fhangfire.correlate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskwasjer%2Fhangfire.correlate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskwasjer%2Fhangfire.correlate/lists"}