c# - 使用 Azure SQL 作为配置存储的 AppFabric 缓存

标签 c# caching azure azure-sql-database appfabric

AppFabric 缓存服务可以使用 Azure SQL 数据库作为缓存集群配置的存储吗?

它绝对支持 SQL Server,如下所述:https://msdn.microsoft.com/en-us/library/ee790826.aspx ,但没有提及 Azure SQL 支持。 Azure SQL 它是 HA 技术,因此看起来像是“有机”方式来存储配置,该配置应该具有高可用性,以保持缓存集群正常运行。

更新。AppFabric 配置 UI 不提供使用 SQL 凭据进行访问的可能性 - 此处仅提供“集成安全性”选项,并且它不是 Azure SQL 服务的选项,而是连接字符串可以通过DistributedCacheService.exe.config手动编辑。

AppFabric SQL Server Configuration UI

<?xml version="1.0" encoding="utf-8"?>
<configuration>
...
  <dataCacheConfig cacheHostName="AppFabricCachingService">
    ...
    <clusterConfig provider="System.Data.SqlClient" connectionString="Server=tcp:azuredbnamehere.database.windows.net,1433;Database=AppFabricConfigHolderTest;User ID=user@azuredbnamehere;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" />
  </dataCacheConfig>
...
</configuration>

更新2。 首先,我已将 AppFabric 配置为使用本地 SQL Server,并且成功了 – 我能够创建新缓存、检查集群状态、管理缓存主机,没有出现任何问题。

然后,我将生成的 AppFabric DB 部署到 Azure SQL 中,并在从安全对象中删除 Windows 用户主体后成功,因为 Azure SQL 不支持它。

然后,我手动修改了 AppFabric 主机配置文件 (DistributedCacheService.exe.config) 中的连接字符串,使其指向 Azure SQL 数据库,而不是本地数据库。

我在这里陷入困境 - 管理 PowerShell 管理单元仍然尝试连接到我的本地数据库实例。看起来有关集群配置存储的信息是重复的,而且不仅 DistributedCacheService.exe.config 知道它。

更新 3. 我已经反编译了为 AppFabric 进行 SQL Server 注册的实用程序 - DistributedCache.SqlConfiguration.exe。我已经意识到“注册”阶段发生了什么:

using (PowerShell powerShellHost = PowerShellHelper.GetPowerShellHost())
{
    powerShellHost.AddCommand("Set-CacheConnectionString");
    powerShellHost.AddParameter("Provider", "System.Data.SqlClient");
    powerShellHost.AddParameter("ConnectionString", connectionString);
    PowerShellHelper.TraceCommands(powerShellHost);
    powerShellHost.Invoke();
    PowerShellHelper.TraceErrorsAndWarnings(powerShellHost);
}

我们来了!

PS > Get-Help Set-CacheConnectionString

NAME Set-CacheConnectionString

SYNOPSIS Persists the connection string for temporary usage. The string can then be retrieved with Get-CacheConnectionString. Note that this command does not change the connection string used by the cache cluster configuration store.

事实证明,集群配置存储信息确实重复。它的存储位置我已经实现了更改执行 Set-CacheConnectionString 的反编译 PowerShell 模块。

using (RegistryKey registryKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\AppFabric\\V1.0\\Temp"))

Temp storage for connection string

更新4. AppFabric缓存管理PowerShell管理单元中最有趣的事情是使用不同的注册表路径(反编译揭示):HKLM\SOFTWARE\Microsoft\AppFabric\V1.0\配置。修改 ConnectionString 属性值后,我终于让 Use-CacheCluster 命令正常工作。

集群也已启动并且现在看起来可以运行。

最佳答案

我们可以使用更新中描述的方法“欺骗”并设置 AppFabric 缓存集群,但是,遗憾的是,结果是确保我们确实使用集成安全性的代码被缝在 PowerShell 管理管理单元中:

// Microsoft.ApplicationServer.Caching.Configuration.ConfigurationBase
...
if (provider.Equals("System.Data.SqlClient"))
{
    ConfigurationBase.ValidateSqlConnectionString(connectionString);
}
...
internal static void ValidateSqlConnectionString(string connectionString)
{
    SqlConnectionStringBuilder sqlConnectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
    if (!sqlConnectionStringBuilder.IntegratedSecurity || !string.IsNullOrEmpty(sqlConnectionStringBuilder.UserID) || !string.IsNullOrEmpty(sqlConnectionStringBuilder.Password))
    {
        int errorCode = 17032;
        string sqlAuthenticationNotSupported = Resources.SqlAuthenticationNotSupported;
        throw new DataCacheException("DistributedCache.ConfigurationCommands", errorCode, sqlAuthenticationNotSupported);
    }
}

如果您尝试 Register-CacheHost(Add-Host 和其他一些命令),您将面临以下错误:

Register-CacheHost : ErrorCode:SubStatus:Only Windows authentication is supportedwith SQL Server provider. Specify a valid connection string for Windows authentication without any User ID or Password.
At line:1 char:1
+ Register-CacheHost -Provider "System.Data.SqlClient" -ConnectionString "Server=t ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Register-CacheHost], DataCacheException
    + FullyQualifiedErrorId : SqlAuthenticationNotSupported,Microsoft.ApplicationServer.Caching.Configuration.Commands.RegisterCacheHostCommand

因此,使用 Azure SQL 作为缓存群集配置持有者的唯一现实选择从头开始编写自定义配置提供程序:https://msdn.microsoft.com/en-us/library/ff718169.aspx或者尝试使用不同名称下实现的 SqlServer 提供程序 (Microsoft.ApplicationServer.Caching.SqlServerCustomProvider) 来欺骗 PowerShell 模块(未测试)。

更新。我已经通过欺骗 AppFabric 并使用不同名称的内置 SQL Server 提供程序来测试该方法。它就像一个魅力。

关于c# - 使用 Azure SQL 作为配置存储的 AppFabric 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578982/

相关文章:

python - pip + Nexus + virtualenv = HTTP 读取超时错误

angular - Azure Devops Angular 环境变量

c# - 如何最好地修改和返回 C# 参数?

ruby-on-rails-3 - 使用 Memcached/Dalli 从缓存中排除图像

C#:如何解决这种循环依赖?

macos - 如何在 Safari 中禁用 DNS 缓存?

azure - 无法撤销从 Azure Active Directory 授予应用程序的权限

azure - Microsoft Azure Active Domain Service (ADDS) 是否支持 PTA?

c# - 无法从我的 C# 控制台应用程序中使用 MongoDB

c# - 为什么我不能在 ASP.NET 2.0 中使用对象初始值设定项?