我已经完成了我在互联网上阅读的所有内容,教程,但似乎没有任何工作!
https://www.google.com/search?q=reliablesqlconnection+azure
我已经安装了所有实验室:
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=6932
NuGets
PM> Install-Package EnterpriseLibrary.WindowsAzure.TransientFaultHandling
PM> Install-Package CommonServiceLocator
我发现解决特定问题的所有配置(仅提一个)。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, ... />
<section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common... />
</configSections>
<RetryPolicyConfiguration defaultRetryStrategy="Fixed Interval Retry Strategy">
<incremental name="Incremental Retry Strategy" />
<fixedInterval name="Fixed Interval Retry Strategy" />
<exponentialBackoff name="Exponential Backoff Retry Strategy" />
</RetryPolicyConfiguration>
<typeRegistrationProvidersConfiguration>
<add sectionName="RetryPolicyConfiguration" name="RetryPolicyConfiguration" />
</typeRegistrationProvidersConfiguration>
</configuration>
我无法让它发挥作用!我一直遇到像
这样的错误Could not load file or assembly 'Microsoft.Practices.ServiceLocation,
OR
The type RetryManager cannot be constructed. You must configure the container to supply this value
OR
Activation error occured while trying to get instance of type RetryManager, key "
或者它在调试时一直在寻找* .cs文件!
越来越多!!
有人在那里!使用简单的azure ReliableSqlConnection示例!我可以下载并运行? 请!最好使用最新的dll?
感谢。
这是我在新WinForm解决方案中的一个简单测试代码
我尝试了很多组合!喜欢
ReliableSqlconnection with ExecuteReader or
SqlConnection with ExecuteReaderWithRetry or
ReliableSqlconnection with ExecuteReaderWithRetry
我无法让它发挥作用!使用SqlConnection和ExecuteReader,工作完美。但是连接不可靠!所以我会不断收到连接错误。
using (var cnn = new ReliableSqlConnection(connString)) { cnn.Open(); using (var cmd = cnn.CreateCommand()) { cmd.CommandText = "SELECT * FROM MyTable"; using (var rdr = cmd.ExecuteReaderWithRetry()) { if (rdr.Read()) { Console.Write(rdr.GetString(1)); } } } }
答案 0 :(得分:3)
企业库6是关于重试逻辑的!没有ReliableSqlConnection。 同样的逻辑适用于所有azure存储服务:
SQL Azure,Windows Azure存储,Windows Azure缓存或Windows Azure Service Bus
您甚至可以使用自己的重试逻辑类和接口 ITransientErrorDetectionStrategy
将其用于所有重试需求所以这是一个工作示例(Console,WinForm,Website,WebMethod):
1.-安装NuGet(V. 6.0) PM&GT; Install-Package EnterpriseLibrary.TransientFaultHandling.WindowsAzure.Storage
2.- WebConfig:
<connectionStrings>
<add name="MyConnectionString" connectionString="Server=tcp:********.database.windows.net,1433;Database=DATABASENAME;User ID=*********;Password=********;Trusted_Connection=False;Encrypt=True;"/>
</connectionStrings>
3.-使用
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling;
4.- De example class
public class ReliableAzureConnection
{
string ConnectionString;
RetryPolicy RetryPolicy;
/// <summary>
/// Initialize the retryPolicy
/// Load the connection string from App.config
/// </summary>
public ReliableAzureConnection()
{
ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
//This means, 3 retries, first error, wait 0.5 secs and the next errors, increment 1 second the waiting
Incremental RetryStrategy = new Incremental(3, TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1));
// You can use one of the built-in detection strategies for
//SQL Azure, Windows Azure Storage, Windows Azure Caching, or the Windows Azure Service Bus.
//You can also define detection strategies for any other services that your application uses.
RetryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(RetryStrategy);
}
public DataTable GetTable(string commandText)
{
DataTable DataTable = null;
DataTable TempDataTable = null;
try
{
TempDataTable = new DataTable();
//This is the function that will retry,
//dont try to make your retry logic your self!
//there are so many error codes. Not all can retry
RetryPolicy.ExecuteAction(() =>
{
// Here you can add any logic!
//1.-Fill DataSet, NonQueries, ExecuteScalar
using (SqlConnection SqlConnection = new SqlConnection(ConnectionString))
{
SqlConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand(commandText, SqlConnection))
{
TempDataTable.Load(SqlCommand.ExecuteReader());
}
}
DataTable = TempDataTable;
TempDataTable = null;
});
}
catch (SqlException ex)
{
//You can manage you own errors, for example bad queries or bad connections.
Debug.WriteLine(ex.Message);
throw;
}
finally
{
if (TempDataTable != null) TempDataTable.Dispose();
}
return DataTable;
}
//Example using ExecuteAction<TResult>
public DataTable GetTableUsingTResult(string commandText)
{
return RetryPolicy.ExecuteAction<DataTable>(() =>
{
DataTable DataTable = new DataTable();
using (SqlConnection SqlConnection = new SqlConnection(ConnectionString))
{
SqlConnection.Open();
using (SqlCommand SqlCommand = new SqlCommand(commandText, SqlConnection))
{
DataTable.Load(SqlCommand.ExecuteReader());
}
}
return DataTable;
});
}
}
5.-致电
ReliableAzureConnection ReliableAzureConnection = new ReliableAzureConnection();
DataTable MyTable = ReliableAzureConnection.GetTable("SELECT * FROM YourTable");
Debug.WriteLine(MyTable.Rows.Count);
我希望它可以帮助那里的人。感谢。
答案 1 :(得分:3)
从.NET 4.6.1开始,SqlConnection类现在已经内置了重试。
Troubleshoot, diagnose, and prevent SQL connection errors and transient errors for SQL Database
用于连接重试的.NET SqlConnection参数
如果您的客户端程序使用.NET Framework类System.Data.SqlClient.SqlConnection连接到Azure SQL数据库,则应使用.NET 4.6.1或更高版本(或.NET Core),以便利用其连接重试功能。该功能的详细信息在这里。
为SqlConnection对象构建连接字符串时,应协调以下参数之间的值:
ConnectRetryCount(默认值为1.范围是0到255.)
ConnectRetryInterval(默认值为1秒。范围为1到60.)
连接超时(默认为15秒。范围为0到2147483647)具体而言,您选择的值应使以下等式为真:
连接超时= ConnectRetryCount * ConnectionRetryInterval例如,如果count = 3,interval = 10秒,则只有29秒的超时不会给系统足够的时间进行第3次和最后的连接重试:29&lt; 3 * 10.
答案 2 :(得分:0)
ReliableSqlConnection在技术上是EL 6的一部分,但它已经过时,不适合关心可伸缩性的现代应用程序,因为该类以及EL 6中所有其他特定于Sql的扩展方法都不支持异步操作,因此,我认为唯一的好解决方案是使用通用重试EL 6逻辑异步方法从ADO.NET中包装 async 方法。