Hangfire Discussion

I am getting an error with the latest version


#1

I upgraded to the latest version today and now all my recurring jobs give this error:

System.Reflection.TargetInvocationException

Exception has been thrown by the target of an invocation.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.UI.Page.get_Server()
at ASP.hangfirerecurringtasks_recurringjobs_aspx…ctor()
— End of inner exception stack trace —
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Hangfire.JobActivator.ActivateJob(Type jobType)
at Hangfire.Common.Job.Activate(JobActivator activator)

Not sure but think I’ve got a dll missing? Can anyone help? Thanks


First time use, Exception all the time
#2

From the stack trace it looks like you are creating your recurring jobs in the constructor of RecurringJobs.aspx and that part of the job is calling the get method of the Server property of the page, which is throwing a null reference exception. Is that correct ?

I’m not sure what the lifecycle of the System.Web.UI.Page.Server HttpServerUtility is, but I’d expect that it is torn down upon an IIS recycle which has likely happened when you updated. (Or possible even after each request)

I’m not sure you’ll be able to get much help without showing some of the code from that page.


#3

Thanks for your reply - that’s correct.

I’m coding in vb but here goes…

              RecurringJob.AddOrUpdate(Sub() BuildQuickReportStatsStepOne(), Cron.Hourly)

And the method is here (it just calls a stored procedure)

<Queue("low")>
Sub BuildQuickReportStatsStepOne()

    ReportFunctions.BuildQuickReportsStatsStepOne(Date.UtcNow)

End Sub

#4

Does the ReportFunctions.BuildQuickReportsStatsStepOne method use any of the HttpServerUtility methods ? (Such as HtmlEncode / HtmlDecode, UrlEncode / UrlDecode or MapPath)

Can you attach a remote debugger and break on the exception ?


#5

No httpserverutility methods.

Error is:

Could not load file or assembly ‘App_Web_frbihwyn, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies.

What on Earth is App_Web_frbihwyn!

Thanks.


#6

Ah I think I know what’s happened here.

Here’s an example of a Job as stored by a recent version of Hangfire in SQL Server:

{
    "Type":"ConsoleSample.Services, ConsoleSample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
    "Method":"Write",
    "ParameterTypes":"[\"System.Char, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\"]",
    "Arguments":"[\"\\\"H\\\"\"]"
}

As you can see, the fully qualified type name is stored when the job is serialised which includes the assembly. Asp.Net web pages are compiled into a temporary assembly and when that page changes, so does the assembly name. (Depending on how you upgrade your site, the existing assembly may be kept around to finish serving existing requests while the new assembly serves new requests.)

You should be able to solve your problem by moving the BuildQuickReportStatsStepOne method / job into its own project / assembly. This will which will not change when your pages are recompiled. The issue will however occur if you change this assembly and either the assembly name or the version number changes. (Or the job’s method signature)


#7

Thanks for the pointers - will let you know if this doesn’t resolve the issue. With the previous version of hangfire, the jobs weren’t serialised in this way.

Note: with previous version of hangfire, jobs were listed in dashboard as BuildQuickReportStatsStepOne etc but now, in new version, as hangfirerecurringtasks_recurringjobs_aspx.BuildQuickReportStatsStepOne.


#8

ahhhh grrrrr I’m still getting the System.Reflection.TargetInvocationException on all recurring tasks. Is it just me?


#9

I’ve resolved this now - by putting all the RecurringTasks into their own assembly (this time correctly).


#10

Glad to hear that, I was getting perplexed after reading your previous update and it still not working.


#11

All recurring tasks are now fine in their new assembly however I am having the same problem with BackgroundJob.Enqueue from various pages. Same error:

   System.Reflection.TargetInvocationException  Exception has been thrown by the target of an invocation. 

Again these were working on previous hangfire version.

Edit: resolved by putting all background tasks into their own class.


#13

Hi Simons,

Could you please elaborate how or what you mean by putting all the methods into their own assembly?