question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Job enqueue doesn't support generic methods

See original GitHub issue

Hangfire should support it on Sql implementation, but generic method parameters are not serialized into the MongoDB document.

Trying to enqueue a call to a generic method I receive Can not find the target method.

Failed
Can not change the state to 'Processing': target method was not found.

System.ArgumentNullException
Value cannot be null.

System.ArgumentNullException: Value cannot be null.
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at Hangfire.Common.TypeExtensions.GetNonOpenMatchingMethod(Type type, String name, Type[] parameterTypes)
   at Hangfire.Storage.InvocationData.Deserialize()

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tmm360commented, Dec 29, 2018

I didn’t write tests, but I wrote very simple cases where this and another (I think very similar and related) issues appear. On Asp.net Core sample project you can add these lines on HomeController:

public ActionResult FailingOnClient()
{
    BackgroundJob.Enqueue(() => FailingOnClientJob<string>());
    return RedirectToAction("Index");
}
    
public ActionResult FailingOnServer()
{
    BackgroundJob.Enqueue<FailingOnServerJob>(job => job.Run<string>());
    return RedirectToAction("Index");
}
    
public void FailingOnClientJob<T>() { }

public class FailingOnServerJob
{
    public void Run<T>() { }
}

If you invoke the FailingOnServer action you will receive the previously mentioned exception by server execution, but if you call the FailingOnClient action you will get this exception directly on client enqueuing:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at Hangfire.Common.TypeExtensions.GetNonOpenMatchingMethod(Type type, String name, Type[] parameterTypes)
   at Hangfire.Common.Job.FromExpression(LambdaExpression methodCall, Type explicitType)
   at Hangfire.BackgroundJobClientExtensions.Create(IBackgroundJobClient client, Expression`1 methodCall, IState state)
   at Hangfire.Mongo.Sample.ASPNetCore.Controllers.HomeController.FailingOnClient() in C:\Users\mirko\Desktop\Hangfire.Mongo\src\Hangfire.Mongo.Sample.ASPNetCore\Controllers\HomeController.cs:line 49
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
0reactions
tmm360commented, Dec 30, 2018

Thank you for your help! You have found my issue, because into my project I don’t have a parameter for each generic type that I pass. I’m using these generic types with an other internal logic, but I’ve actually solved invoking an other not-generic proxy method that takes these types as arguments, and use reflection for invoke the generic one.

If hangfire could serialize also generic types it would be great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Job enqueue doesn't support generic methods · Issue #165
Trying to enqueue a call to a generic method I receive Can not find the target method. Failed Can not change the state...
Read more >
Using Hangfire: generic Enqueue<T> method throws ...
SqlServer packages installed. In my main method I enqueue a background job like this: BackgroundJob.Enqueue<Test>((t) => Console.WriteLine ...
Read more >
System.InvalidOperationException when Enqueue Generic ...
I'm getting following exception. The type Harmony.Admin.Service.IEmailService does not contain a method with signature SendAsync(String). System ...
Read more >
How to write generic classes and methods in Java
This Java Generics tutorial helps you design highly general and reusable libraries with generic classes and methods.
Read more >
Basics of Generics in Scala
We'll go through the steps of writing generic classes and methods and explore the generic types available in the standard Scala library.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found