gpt4 book ai didi

c# - 使用 ReliableSqlConnection 和 Azure 的工作示例

转载 作者:太空狗 更新时间:2023-10-30 00:51:58 28 4
gpt4 key购买 nike

我已经完成了在互联网上阅读的所有内容、教程,但似乎没有任何效果!

https://www.google.com/search?q=reliablesqlconnection+azure

http://geekswithblogs.net/ScottKlein/archive/2012/01/27/understanding-sql-azure-throttling-and-implementing-retry-logic.aspx

我已经安装了所有的动手实验室:

http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=6932

NuGet 人

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,

或者

The type RetryManager cannot be constructed. You must configure the container to supply this value

或者

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));
}
}
}
}

最佳答案

企业库 6 就是关于重试逻辑的!不再有 ReliableSqlConnection。同样的逻辑适用于所有 Azure 存储服务:

SQL Azure、Windows Azure 存储、Windows Azure 缓存或 Windows Azure 服务总线

您甚至可以使用您自己的重试逻辑类和接口(interface)ITransientErrorDetectionStrategy来满足所有重试需求

这是一个工作示例(控制台、WinForm、网站、WebMethod):

1.- 安装 NuGet (V. 6.0)PM> 安装包 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.- 示例类

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);

我希望它能帮助那里的人。谢谢。

关于c# - 使用 ReliableSqlConnection 和 Azure 的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172800/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com