From 430946e90afdf5dc7733bb37fdf629734799769d Mon Sep 17 00:00:00 2001 From: yiyun Date: Thu, 25 Jan 2024 18:25:52 +0800 Subject: [PATCH] fix(plugins/hangfireplugin/hangfireplugin.cs): job execution not working in dynamically loaded ass --- plugins/HangfirePlugin/HangfirePlugin.cs | 33 +++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/plugins/HangfirePlugin/HangfirePlugin.cs b/plugins/HangfirePlugin/HangfirePlugin.cs index 827d64379..66598a275 100644 --- a/plugins/HangfirePlugin/HangfirePlugin.cs +++ b/plugins/HangfirePlugin/HangfirePlugin.cs @@ -73,10 +73,41 @@ public void ConfigureServices(IServiceCollection services) .UseSQLiteStorage(nameOrConnectionString: dbFilePath, options: new SQLiteStorageOptions() { - })); + }) + // https://github.com/HangfireIO/Hangfire/issues/470 + .UseTypeResolver((typeName) => + { + try + { + return Hangfire.Common.TypeHelper.DefaultTypeResolver(typeName); //try using default resolver + } + catch (Exception) //if couldn't find + { + //get it from AppDomain loaded assemblies. + var reqType = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes().Where(x => typeName.StartsWith(x.FullName))).FirstOrDefault(); + + if (reqType != null) + return reqType; + + throw; + } + }) + ); // Add the processing server as IHostedService services.AddHangfireServer(); + + // https://github.com/HangfireIO/Hangfire/issues/470 + //Hangfire.Common.TypeHelper.CurrentTypeResolver += typeName => + //{ + // if (typeName.Contains("MyCompany.Services.Messaging")) + // { + // // Load assembly from the specific place and use it to find the type + // return Assembly.GetType(typeName); + // } + + // return TypeHelper.DefaultTypeResolver(typeName); + //}; } public int ConfigureServicesOrder => -999;