From cc73928ed075a5a702a57ad6203adaa1f77acc63 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Fri, 21 May 2021 02:10:18 +0200 Subject: [PATCH] Fixed MODM-76 --- .../Extensions/HangfireGlobalConfigExtension.cs | 17 +++++++++++++++++ .../Tasks/HangfireTaskRunner.cs | 4 ---- src/MongODM/ServiceCollectionExtensions.cs | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs diff --git a/src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs b/src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs new file mode 100644 index 00000000..7a88ce78 --- /dev/null +++ b/src/MongODM.Hangfire/Extensions/HangfireGlobalConfigExtension.cs @@ -0,0 +1,17 @@ +using Etherna.ExecContext.AsyncLocal; +using Etherna.MongODM.HF.Filters; + +namespace Hangfire +{ + public static class HangfireGlobalConfigExtension + { +#pragma warning disable IDE0060 // Remove unused parameter + public static void UseMongODM(this IGlobalConfiguration config) +#pragma warning restore IDE0060 // Remove unused parameter + { + // Add a default execution context running with any Hangfire task. + // Added because with asyncronous task, unrelated to requestes, there is no an alternative context to use with MongODM. + GlobalJobFilters.Filters.Add(new AsyncLocalContextHangfireFilter(AsyncLocalContext.Instance)); + } + } +} diff --git a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs index dabae0cb..8cfddb88 100644 --- a/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs +++ b/src/MongODM.Hangfire/Tasks/HangfireTaskRunner.cs @@ -36,10 +36,6 @@ public HangfireTaskRunner( { this.backgroundJobClient = backgroundJobClient; this.mongODMOptions = mongODMOptions; - - // Add a default execution context running with any Hangfire task. - // Added because with asyncronous task, unrelated to requestes, there is no an alternative context to use with MongODM. - GlobalJobFilters.Filters.Add(new AsyncLocalContextHangfireFilter(AsyncLocalContext.Instance)); } // Methods. diff --git a/src/MongODM/ServiceCollectionExtensions.cs b/src/MongODM/ServiceCollectionExtensions.cs index afa91e21..e55ba23b 100644 --- a/src/MongODM/ServiceCollectionExtensions.cs +++ b/src/MongODM/ServiceCollectionExtensions.cs @@ -70,6 +70,7 @@ private static void AddHangfire( // Add hangfire. services.AddHangfire(options => { + options.UseMongODM(); options.UseMongoStorage(hangfireOptions.ConnectionString, hangfireOptions.StorageOptions); }); }