从Hangfire内部处理异常 [英] Handle Exceptions from inside Hangfire

查看:113
本文介绍了从Hangfire内部处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我正在计划一个BackgroundJob在应用程序启动几秒钟后运行.作业完成后,我将再次安排相同的BackgroundJob在随机的几秒钟后运行,并且继续进行并且永不停止.

I have a scenario, I am scheduling a BackgroundJob to run after some random seconds on application start. After the job is performed then I am again scheduling the same BackgroundJob to run after some random seconds and it goes on and never stops.

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

What I want is if any exception occurs in Hangfire during this continuous process I want to shut-down my asp.net core app.

例如,在此过程中,我停止了SQL Server.因此,hangfire给出了明显的例外情况

for example, while this process is happening I stop the SQL Server. So hangfire giving this exception that is obvious

System.Data.SqlClient.SqlException:建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接.(提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接)

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:系统找不到指定的文件
---内部异常堆栈跟踪的结尾---
在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,布尔值allowCreate,仅布尔值OneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal& Connection)在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource1重试,DbConnectionOptions userOptions,DbConnectionInternal&连接)在System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,TaskCompletionSource1重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& Connection)在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal处(DbConnection externalConnection,DbConnectionFactory connectionFactory,TaskCompletionSource1重试,DbConnectionOptions userOptions)在System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1重试)在System.Data.SqlClient.SqlConnection.Open()在Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection()在Hangfire.SqlServer.SqlServerStorage.UseConnection [T](Func`2 func)在Hangfire.SqlServer.SqlServerConnection.RemoveTimedOutServers(TimeSpan timeOut)在Hangfire.Server.ServerWatchdog.Execute(BackgroundProcessContext上下文)在Hangfire.Server.AutomaticRetryProcess.Execute(BackgroundProcessContext上下文)

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)

所以我不知道在哪里处理此异常,因此,如果发生此异常,我可以停止我的应用程序.

So I don't know where to handle this exception, so that I can stop my application if this exception occurs.

推荐答案

我已经通过检查存储是否已附加到hangfire来解决了这个问题.

I have solved the problem by checking is storage is attached to hangfire or not.

        try
        {
            IStorageConnection connection = JobStorage.Current.GetConnection();
            return true;
        }
        catch
        {
            return false;
        }

这篇关于从Hangfire内部处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆