skip to Main Content

I’m working on a Asp.Net project where I’m trying to add "Hangfire" library for background jobs.
I’ve installed all required packages according doccumentation and also created the test database.

I’ve also added the required startup methods in Global.asax.vb (had to convert from c#, given in the example, to vb.net) so my file looks like this:

Imports Hangfire
Imports Hangfire.SqlServer

Public Class Global_asax
    Inherits HttpApplication

    Sub Application_Start(sender As Object, e As EventArgs)
        ' Fires when the application is started
        Try
            HangfireAspNet.Use(GetHangfireServers)
        Catch ex As Exception
            Debug.Assert(False, "Not Yet Ready")
        End Try
    End Sub

    Private Iterator Function GetHangfireServers() As IEnumerable(Of IDisposable)
        GlobalConfiguration.Configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170).UseSimpleAssemblyNameTypeSerializer().UseRecommendedSerializerSettings().UseSqlServerStorage("Data Source=xxx,00000;Initial Catalog=xxx;User ID=xxx;Password=xxx", New SqlServerStorageOptions With {
            .CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
            .SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
            .QueuePollInterval = TimeSpan.Zero,
            .UseRecommendedIsolationLevel = True,
            .DisableGlobalLocks = True
        })
        Yield New BackgroundJobServer()
    End Function

End Class

And the

HangfireAspNet.Use(GetHangfireServers)

line is throwing the next exception:

Unable to cast object of type ‘VB$StateMachine_6_GetHangfireServers’ to type ‘System.Func1[System.Collections.Generic.IEnumerable1[System.IDisposable]]

I’ve verified that the connection string is OK and it connects to the test database with no problems, but I’m stuck regarding the exception.

Any help?

2

Answers


  1. Chosen as BEST ANSWER

    This is how I solved my problem, just putting the function inside the Use():

    Public Shared Sub Init()
        Dim connStr = "Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx"
        HangfireAspNet.Use(
            Iterator Function()
                GlobalConfiguration.Configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170).UseSimpleAssemblyNameTypeSerializer().UseRecommendedSerializerSettings().UseSqlServerStorage(connStr, New SqlServerStorageOptions With {
                    .CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    .SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    .QueuePollInterval = TimeSpan.Zero,
                    .UseRecommendedIsolationLevel = True,
                    .DisableGlobalLocks = True
                })
                Yield New BackgroundJobServer()
            End Function
        )
    End Sub
    

  2. In VB you have to use AddressOf in your proc call, i.e.:

    HangfireAspNet.Use(AddressOf GetHangfireServers)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search