簡體   English   中英

VB.Net webforms App:啟動時 Hangfire 后台作業庫“無法投射對象”錯誤

[英]VB.Net webforms App: Hangfire background jobs library "Unable to cast object" error at startup

我正在做一個 Asp.Net 項目,我正在嘗試為后台作業添加“Hangfire”庫。 我已經根據文檔安裝了所有必需的包,還創建了測試數據庫。

我還在 Global.asax.vb 中添加了所需的啟動方法(必須從示例中給出的 c# 轉換為 vb.net),因此我的文件如下所示:

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

HangfireAspNet.Use(GetHangfireServers)

行拋出下一個異常:

無法將“VB$StateMachine_6_GetHangfireServers”類型的 object 轉換為類型“System.Func 1[System.Collections.Generic.IEnumerable 1[System.IDisposable]]

我已經驗證連接字符串沒問題,並且可以毫無問題地連接到測試數據庫,但我對異常一無所知。

有什么幫助嗎?

這就是我解決問題的方法,只需將函數放在 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

在 VB 中,您必須在 proc 調用中使用 AddressOf,即:

HangfireAspNet.Use(AddressOf GetHangfireServers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM