c# - 异步 Azure SQL 调用的 ReliableSqlConnection 替代方法

标签 c# azure ado.net azure-sql-database enterprise-library

我们目前对 Azure SQL 数据库的所有调用都使用 ReliableSqlConnection。这很好用。 但不幸的是,Enterprise Library 是 2013 年的,似乎不再开发了。但主要问题是,不支持async 数据库调用。

我一直在搜索 SO 和其他地方,但到目前为止我唯一能找到的是 Polly。甚至Polly似乎没有被广泛使用,我还没有找到 SQL Server 的例子。 我知道 ADO.NET 支持重试 SqlConnection.Open(),但不支持命令,因此它不是真正的解决方案。我有点惊讶,我们似乎是唯一遇到这个问题的人?

最佳答案

鲜为人知的事实是, transient 容错现在内置于连接字符串 (.NET 4.6.1+) 中。 This document说:

If your client program connects to to Azure SQL Database by using the .NET Framework class System.Data.SqlClient.SqlConnection, you should use .NET 4.6.1 or later (or .NET Core) so you can leverage its connection retry feature. Details of the feature are here.

When you build the connection string for your SqlConnection object, you should coordinate the values among the following parameters:

ConnectRetryCount (Default is 1. Range is 0 through 255.)
ConnectRetryInterval (Default is 1 second. Range is 1 through 60.)
Connection Timeout (Default is 15 seconds. Range is 0 through 2147483647)

Specifically, your chosen values should make the following equality true:

Connection Timeout = ConnectRetryCount * ConnectionRetryInterval

For example, if the count = 3, and interval = 10 seconds, a timeout of only 29 seconds would not quite give the system enough time for its 3rd and final retry at connecting: 29 < 3 * 10.

关于c# - 异步 Azure SQL 调用的 ReliableSqlConnection 替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45171109/

相关文章:

c# - 自定义 StronglyTyped BindingSource 项添加

azure - 从外部库静态解析 Azure Functions 中的 HttpContext/HttpRequestMessage

c# - TSQL 从列可能不存在的临时表插入

c# - Firebird 数据读取器

entity-framework - "Pluralize or singularize generated object names"设置是什么意思?

c# - 为什么要使用两个IPEndPoint?

c# - 在不使用 foreach 的情况下在列表中查找对象

c# - 如何使用 C#-WPF-Entity-Framework Code First 应用程序创建数据库备份?

c# - Azure:无需定义附加配置即可获取服务证书

azure - 托管私有(private) Conda 包 - URL 包含查询字符串