asp.net-core - 处理 Hangfire 内部的异常

标签 asp.net-core asp.net-core-1.0 hangfire

我有一个场景,我正在安排一个BackgroundJob在应用程序启动时随机几秒后运行。执行作业后,我再次安排相同的BackgroundJob在随机几秒后运行,并且它会继续下去并且永远不会停止。

我想要的是,如果在此连续过程中 Hangfire 发生任何异常,我想关闭我的 asp.net core 应用程序。

例如,当这个过程发生时,我停止 SQL Server。因此,hangfire 给出了这个明显的异常

System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

System.ComponentModel.Win32Exception: The system cannot find the file specified
--- End of inner exception stack trace ---
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection() at Hangfire.SqlServer.SqlServerStorage.UseConnection[T](Func`2 func) at Hangfire.SqlServer.SqlServerConnection.RemoveTimedOutServers(TimeSpan timeOut) at Hangfire.Server.ServerWatchdog.Execute(BackgroundProcessContext context) at Hangfire.Server.AutomaticRetryProcess.Execute(BackgroundProcessContext context)

所以我不知道在哪里处理这个异常,这样如果发生这个异常我可以停止我的应用程序。

最佳答案

我认为你有两个选择:

  1. try catch 在您使用 Hangfire 调用的方法中
  2. 使用 IElectStateFilter,特别是如果您希望任何 Hangfire 作业都具有相同的行为

第一个选项非常简单:

public void MyMethod() {
    try {
        //your code
    }
    catch(Exception ex) { 
        //stop application here
    }
}

BackgroundJob.Enqueue(() => MyMethod());

第二个选项如下:

  1. 创建属性类:

    public class MyFilterAttribute : JobFilterAttribute, IServerFilter, IElectStateFilter 
    {
        public void OnStateElection(ElectStateContext context)
        {
            var failedState = context.CandidateState as FailedState;
            if (failedState != null)
            {
                //job has failed, stop application here
            }
        }
    }
    
  2. 注册过滤器:

    GlobalConfiguration.Configuration.UseFilter(new MyFilterAttribute());
    
  3. 将属性添加到您的方法

    [MyFilter]
    public void MyMethod() {
        //your code
    }
    
  4. 照常调用您的代码:

    BackgroundJob.Enqueue(() => MyMethod());        
    

参见documentation关于过滤器。

关于asp.net-core - 处理 Hangfire 内部的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42924624/

相关文章:

c# - 我可以使用 Microsoft Dotnet CLI 构建 ASP.NET Core Web 应用程序吗?

asp.net-core - ASP.NET Core中的ServicePointManager

jquery - 使用 ASP.NET MVC 6 中的文件进行 Ajax 表单提交

c# - 在 Hangfire 仪表板中显示日志?

azure - 在同一reactjs应用程序和aspnet core web api中混合Azure AD和Azure AD B2C身份验证

c# - 获取数据访问层内的 Db 上下文

c# - 将类添加到 Blazor(Razor) 验证

.net - HangFire 作为 .NET 6 的 Windows 服务

c# - Hangfire 作业导致整个应用程序崩溃;我们如何让 Hangfire 来处理错误?

c# - .NET core Web API,如何更改使用 IAsyncEnumerable 时返回的 XML 的根名称?