I get the below exception:
StackTrace:
at Hangfire.Common.Job..ctor(Type type, MethodInfo method, Object[] args)
at Hangfire.Common.Job.FromExpression(LambdaExpression methodCall, Type explicitType)
at Hangfire.Common.Job.FromExpression(Expression`1 methodCall)
at Hangfire.BackgroundJobClientExtensions.Create(IBackgroundJobClient client, Expression`1 methodCall, IState state)
at Hangfire.BackgroundJobClientExtensions.Enqueue(IBackgroundJobClient client, Expression`1 methodCall)
at Hangfire.BackgroundJob.Enqueue(Expression`1 methodCall)
at Program.<Main>$(String[] args) in ...
Message: Value cannot be null. (Parameter 'method')
Source: “Hangfire.Core”
ParamName: “Method”
I have made a Minimal Code Example that throws the exception using these versions:
Hangfire.Core: 1.7.33
Hangfire.InMemory: 0.3.6 (Same error occurs using other providers, just using this for demo below)
DotNet: 7.0
using Hangfire;
using Microsoft.Extensions.DependencyInjection;
Console.WriteLine("Hello, World!");
ServiceCollection services = new ServiceCollection();
services.AddTransient<MyBaseClassService>();
services.AddTransient<IServiceInterface<MyBaseClass>>((s) => s.GetRequiredService<MyBaseClassService>());
services.AddTransient<IServiceInterface<MyDerivedClass>>((s) => s.GetRequiredService<MyBaseClassService>());
var sp = services.BuildServiceProvider();
GlobalConfiguration.Configuration.UseInMemoryStorage();
using (var server = new BackgroundJobServer())
{
IServiceInterface<MyBaseClass> service = new MyBaseClassService();
MyDerivedClass myClass = new MyDerivedClass();
BackgroundJob.Enqueue(() => Console.WriteLine("Background!"));
var classServices = sp.GetServices<IServiceInterface<MyDerivedClass>>();
foreach(var myService in classServices)
{
myService.MyMethod(myClass); //This works
}
try
{
foreach (var myService in classServices)
{
BackgroundJob.Enqueue(() => myService.MyMethod(myClass)); //This fails
}
}
catch(Exception ex)
{
}
Console.ReadKey();
}
class MyBaseClass
{
public string BaseProp { get; set; }
}
class MyDerivedClass : MyBaseClass
{
public int MyProperty { get; set; }
}
interface IServiceInterface <in T> where T : MyBaseClass
{
public Task MyMethod(T input);
}
class MyBaseClassService : IServiceInterface<MyBaseClass>
{
public Task MyMethod(MyBaseClass input)
{
return Task.CompletedTask;
}
}
Is there any reason why these method calls can’t be enqueued to Hangfire?