How to use Hangfire with VB.NET

Hello Everyone,

I am able to start the Hangfire with vb.net and here is my code in case if someone needs it

Imports Hangfire
Imports Hangfire.SqlServer
Imports Microsoft.Owin
Imports Owin

<Assembly: OwinStartup(GetType(Scheduler.Startup))> 

Namespace Scheduler
    ''' <summary>
    ''' Configures the hangfire.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Startup
        Public Sub Configuration(app As IAppBuilder)
            Dim act = Sub(config As IBootstrapperConfiguration)
                          config.UseSqlServerStorage("dbConnection")
                          config.UseServer()
                      End Sub

            app.UseHangfire(act)
        End Sub
    End Class
End Namespace

Note in the line

config.UseSqlServerStorage("dbConnection")

I have used the name of my connection string in the web.config file.

1 Like

My aim was to configure the Hangfire in a central Api assembly so that I dont have to add the hangfire to all other assemblies. I am posting my solution here for others who need to do something similar

Blow is the interface and the class I added in the Api assembly

    ''' <summary>
    ''' Interface should be implemented by a class if it wants to run
    ''' </summary>
    ''' <remarks></remarks>
    Public Interface IJob

        Sub DoJob()

    End Interface


 Public Class Scheduler

        ''' <summary>
        ''' Adds a background job to be executed. 
        ''' </summary>
        ''' <typeparam name="T"></typeparam>
        ''' <param name="sJob"></param>
        ''' <remarks></remarks>
        Public Shared Sub AddBackgroundJob(Of T)()
            BackgroundJob.Enqueue(Of T)(Sub(job) CType(job, IJob).DoJob())
        End Sub
End Class

Then in one of the other assemblies I did this to add jobs to Hangfire, currently it just contains a method to add a Fire and Forget job.

Class StatScheduler
        Implements IJob

        Public Sub DoJob() Implements IJob.DoJob
           ' ToDo
        End Sub
End Class

Then finally we send job to the Api form the target assembly like this

 Scheduler.AddBackgroundJob(Of StatScheduler)()

@farid_masood, thank you for the code snippets that shows how to start Hangfire with VB.NET! I’ve replaced the first post of this topic to make it easy to find the answer.

Did you intentionally leave out the sJob parameter? Or is it missing in the Sub declaration?