3

所以我在实体框架“任何”事务期间收到 Win32Exception。db 机器处于脱机状态,因此这是意料之中的。这是一个监控应用程序,所以我想记录机器不可用,但它没有捕捉到错误。

错误是:(Elmah报告)

System.ComponentModel.Win32Exception 找不到网络路径

System.Data.SqlClient.SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)---> System.ComponentModel.Win32Exception (0x80004005):在 System.Data.SqlClient.SqlInternalConnection.OnError( SqlException 异常,Boolean breakConnection,Action`1 wrapCloseInAction)

好的,这是我的(伪)代码:

if(Monitor.TryEnter(Variables))
try
{
    foreach(var thing in things)
    {
        try
        {
            using(var ctx = new context())
            {
                Do Stuff... 
                if(thing.Any()) <-- Error
                Do more stuff... like look for problems.
            }
        }
        catch(Win32Exception)
        {
            Add exception to list of problems.;
        }
        catch(exception)
        {
            throw; //I was just seeing if this would help, it didn't
        }
     }
     Return list of problems
}
catch(Exception ex)
{
    Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
finally
{
    Monitor.Exit(variables)
}

所以我希望你能看到 Win32Exception 应该被捕获,但事实并非如此——Elmah 正在报告它。在数据库中发现的错误正在被正确定位和报告。这一切都有效,直到发生此 Win32Exception 然后它似乎忽略了我的捕获。

4

0 回答 0