Efcore: DatabaseFacade.CanConnect throws instead of returning false

Created on 13 Oct 2019  路  8Comments  路  Source: dotnet/efcore

On SQL Server, if the database server is down, an SqlException is thrown instead of returning false. It seems like we should catch all exceptions internally and return false instead.

closed-fixed type-bug

All 8 comments

@rynowak what would be the expected behavior here from the ASP.NET health check perspective? It seems problematic for this method to sometimes throw and sometimes return false - what do other health checks do?

@rynowak ping

@rynowak Pinging you now you've been right-side-up for a while. What is the general guidance on health checks and exceptions. Should any error report back as false, or should unexpected errors be surfaced as exceptions?

I would expect it to return false 馃榿

Health checks are an abstraction/isolation layer. A health check should return "Unhealthy" if the check fails, that's preferrable to an exception. If the check throws an exception that will be logged differently.

Since checks are are abstraction layer, it's also reasonable for a check to handle exceptions from whatever it's checking. Some of them do that because that's the only option. In the case that you want to have the exception convey something meaningful and log that too, it's an option.

In general though returning false is preferrable to throwing.

@rynowak Thanks!

Hello @roji , we are using EF Core Dbcontext 3.1.0, and the dbContext.Database.CanConnect() still throws an exception if the server is offline. I thought this was pushed to master. Thoughts?

@antoinecfmws this was done too late for 3.1.0, so will be released for 5.0.0 (and its previews). This is why this issue is in the 5.0.0 milestone.

Makes sense now, thank you! Sorry to bother @roji

Was this page helpful?
0 / 5 - 0 ratings