First time use, Exception all the time

I just started using Hangfire in order to process a large number of email sending tasks (bukmail)
Tried to setup a simple webapplication as well as website projects. Both of them do not work

My code is pretty simple:

Startup class
Imports Hangfire
Imports Microsoft.Owin
Imports Owin
Imports Hangfire.SqlServer
<Assembly: OwinStartup(GetType(MyWebApplication.Startup))>
Namespace MyWebApplication
Public Class Startup
Public Sub Configuration(app As IAppBuilder)
End Sub
End Class
End Namespace

Imports Hangfire
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundJob.Enqueue(Function() dolongstuff())
End Sub
Public Function dolongstuff() As Integer
For i As Integer = 0 To 10000
Next i
Return 42
End Function
End Class

Everytime, i can see the task being queued but i get the following exception on the Hangfire dashboard job details:

Failed An exception occurred during job activation.

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.webform1_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)


This is the same issue that someone else had not so long ago: I am getting an error with the latest version

The solution is to put your jobs into their own assembly. (I.e. The method: dolongstuff())

yep, i figured that out and put it in it’s own class and it worked

Could u share code example of solution?

1 Like