< img src ="https://images.idgesg.net/images/article/2020/09/resilient_resilience_weed-growing-in-desert_by-barcin-via-getty-images-100856443-large.jpg?auto=webp&quality=85,70"alt=""> A top quality application should be stable, performant, scalable, and dependable. The dependability of an application depends upon numerous factors, however one of the most crucial is resiliency, or the ability to stand up to failures or faults throughout run time. In this short article, we ll see how we can bring resiliency to database connections in ASP.NET Core using Entity Framework Core.EF Core has a function called connection resiliency that immediately retries failed database commands to preserve connectivity throughout transient errors or network instability. By encapsulating the logic for identifying failures and retrying commands, this function enables us to design execution prepare for different database failure situations.To utilize the code examples supplied in this short article, you must have Visual Studio 2022 set up in your system. If you don t already have a copy, you can download Visual Studio 2022 here. Produce an ASP.NET Core Web API task in Visual Studio 2022 To develop an ASP.NET Core Web API job in Visual Studio 2022, follow the actions described below. Launch the Visual Studio 2022 IDE. Click Produce new
task. In the Develop brand-new project window, choose ASP.NET Core Web API from the list of templates displayed. Click Next.
them if the errors are deemed temporal. An execution method permits designers to ensure their applications can gracefully recover from short-term mistakes without requiring human intervention. You can produce an execution strategy utilizing the CreateExecutionStrategy method as shown in the code snippet provided below.var technique=_ context.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async()=> ); As you can see from the following code example, an execution method is typically defined in the OnConfiguring technique of your custom DbContext class. protected override space OnConfiguring(DbContextOptionsBuilder optionsBuilder)optionsBuilder. UseSqlServer (@”Server=mssqldb; Database=Test; Trusted_Connection=True “, choices= > options.EnableRetryOnFailure()); If you re using Azure SQL Database, EF Core already offers the resiliency and retry reasoning for your database. Nevertheless, you should allow the EF Core execution strategy for each DbContext connection your application makes if you want to leverage EF Core connection resiliency. The following code snippet shows how you can enable durable SQL connections using EF Core that are retried whenever the database connection goes down.builder.Services.AddDbContext(alternatives=> options.UseSqlServer
(builder.Configuration [“IDGConnectionString”], sqlServerOptionsAction: sqlOptions => sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds (45) ););); Usage connection resiliency with deals in EF Core If you ve allowed retries in EF Core, every call to the SaveChanges method will be retried as a system if a database connection failure occurse. Nevertheless, if you carry out a deal block in your application s code, utilizing the BeginTransaction technique, you must conjure up an execution technique explicitly using a delegate to make sure that every operation inside the deal is performed. This is shown in the code bit provided below.var technique =db.Database.CreateExecutionStrategy (); strategy.Execute (()=> using var context =new LibraryContext ();
utilizing var deal=context.Database.BeginTransaction(< ); Manage database connection failures in ASP.NET Core When working with database connections utilizing EF Core, you should manage prospective connection failures by catching exceptions and retrying the database operations. Consider the following entity class called Customer.public class Client public int Id get; set; public string FirstName =string.Empty; public string LastName = string.Empty; public string Address get; set;=string.Empty; public string City get; set;=string.Empty; public string PostalCode get; set;=string.Empty; public string Country
. get; set;=string.Empty; public string Phone get; set;= string.Empty; The following code listing shows the DbConnectService class that carries out the IDbConnectService user interface and demonstrates how connection failures and retry operations can be carried out. public class DbConnectService: IDbConnectService The source code of the IDbConnectService user interface is given below.public interface IDbConnectService Produce a CustomDbContext class in EF Core As noted above, you will usually define your execution strategy in the OnConfiguring technique of your custom-made DbContext class. The following code listing illustrates the CustomDbContext class that extends the DbContext class of EF Core and implements the OnConfiguring and OnModelCreating methods.public class CustomContext: DbContext A terrific method to handle database connection failures and make your application durable is to utilize Polly, a fault-handling library for.NET. You can utilize Polly to execute breaker performance so that any database connection failures in the application are handled gracefully. I ll discuss using Polly to carry out circuit-breakers in a future post here.
Copyright © 2024 IDG Communications, Inc. Source