Cagaya Coper
Cagaya Coper

Reputation: 75

How to make sure the database server is always available while using Entity Framework?

I am using Entity Framework in VS2015 and I wanted to make sure that every time I get/delete data from the entities the database server is always in a good connection state.

I know some of the ways to handle the exception, but is there a better way where I do not have to add try catch to every function? Thanks.

Upvotes: 1

Views: 74

Answers (1)

Yogi
Yogi

Reputation: 9749

For one of our projects we have used Polly to handle the transient errors while connecting to database, and loved it.

Polly is a .NET library that allows developers to express transient exception- and fault-handling policies such as Retry, Retry Forever, Wait and Retry or Circuit Breaker in a fluent and thread-safe manner.

We have configured the Polly to handle the exception for connection failure and retried it in our case

    // Retry multiple times, calling an action on each retry  
    // with the current exception and retry count Policy
        .Handle<ExceptionIWAntToHandle>()
        .Retry(3, (exception, retryCount) =>
        {
            // do something 
        });

And yes as suggested in comments, have a centralized location for this.

Upvotes: 1

Related Questions