I want to launch a method in a sub object of a service in background with Hangfire. So this is what I do.
BackgroundJob.Enqueue<IMyService>(myService => myService.SubObject.MyPublicMethodAsync());
But it throw an exception because MyPublicMethodAsync
is in SubObject
and not in IMyService
because of this validation code in HangFire :
if (!method.DeclaringType.GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
{
throw new ArgumentException(
$"The type `{method.DeclaringType}` must be derived from the `{type}` type.",
typeParameterName);
}
https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.Core/Common/Job.cs (line 391)
My current workaround is to do that :
public Task DoWhatIWant()
{
return _myService.SubObject.MyPublicMethodAsync();
}
And
BackgroundJob.Enqueue(() => DoWhatIWant());
But it’s very ugly so do you know a proper way to do that ?