Skip to content

Commit

Permalink
Refactor the configuration reader to prepare for connectionName suppo…
Browse files Browse the repository at this point in the history
…rt (#1881)

* Refactor the configuration reader to prepare for connectionName support

* Fix build
  • Loading branch information
vicancy authored Dec 22, 2023
1 parent 5bbbe1a commit 39e4886
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public ServiceOptions()
var count = ConfigurationManager.ConnectionStrings.Count;
string connectionString = null;
var endpoints = new List<ServiceEndpoint>();
var connectionStringKeyPrefix = $"{Constants.Keys.ConnectionStringDefaultKey}:";
for (var i = 0; i < count; i++)
{
var setting = ConfigurationManager.ConnectionStrings[i];
Expand All @@ -116,7 +117,7 @@ public ServiceOptions()
{
connectionString = setting.ConnectionString;
}
else if (setting.Name.StartsWith(Constants.Keys.ConnectionStringKeyPrefix) && !string.IsNullOrEmpty(setting.ConnectionString))
else if (setting.Name.StartsWith(connectionStringKeyPrefix) && !string.IsNullOrEmpty(setting.ConnectionString))
{
endpoints.Add(new ServiceEndpoint(setting.Name, setting.ConnectionString));
}
Expand All @@ -131,7 +132,7 @@ public ServiceOptions()
{
connectionString = ConfigurationManager.AppSettings[key];
}
else if (key.StartsWith(Constants.Keys.ConnectionStringKeyPrefix))
else if (key.StartsWith(connectionStringKeyPrefix))
{
var value = ConfigurationManager.AppSettings[key];
if (!string.IsNullOrEmpty(value))
Expand Down
11 changes: 3 additions & 8 deletions src/Microsoft.Azure.SignalR.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ internal static class Constants
public static class Keys
{
public const string AzureSignalRSectionKey = "Azure:SignalR";
public const string ServerStickyModeDefaultKey = "Azure:SignalR:ServerStickyMode";
public const string ConnectionStringDefaultKey = "Azure:SignalR:ConnectionString";
public const string ApplicationNameDefaultKey = "Azure:SignalR:ApplicationName";
public const string AzureSignalREnabledKey = "Azure:SignalR:Enabled";
public const string AzureSignalREndpointsKey = "Azure:SignalR:Endpoints";
public const string ConnectionStringDefaultKey = $"{AzureSignalRSectionKey}:ConnectionString";
public const string AzureSignalREnabledKey = $"{AzureSignalRSectionKey}:Enabled";
public const string AzureSignalREndpointsKey = $"{AzureSignalRSectionKey}:Endpoints";

public static readonly string ConnectionStringSecondaryKey =
$"ConnectionStrings:{ConnectionStringDefaultKey}";
public static readonly string ConnectionStringKeyPrefix = $"{ConnectionStringDefaultKey}:";
public static readonly string ApplicationNameDefaultKeyPrefix = $"{ApplicationNameDefaultKey}:";
public static readonly string ConnectionStringSecondaryKeyPrefix = $"{ConnectionStringSecondaryKey}:";
}

public const string AsrsMigrateFrom = "Asrs-Migrate-From";
Expand Down
34 changes: 27 additions & 7 deletions src/Microsoft.Azure.SignalR/ServiceOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,42 @@ private T GetConfiguredEnum<T>(string value, T defaultValue) where T : struct

private (string AppName, string ConnectionString, ServiceEndpoint[] Endpoints, ConfigurableServiceOptions configurableOptions) ParseConfiguration()
{
var options = _configuration.GetSection(Constants.Keys.AzureSignalRSectionKey).Get<ConfigurableServiceOptions>();
var sectionKey = Constants.Keys.AzureSignalRSectionKey;
var options = _configuration.GetSection(sectionKey).Get<ConfigurableServiceOptions>();

// For backward compatability, first read from prefix
var appName = _configuration[Constants.Keys.ApplicationNameDefaultKeyPrefix] ?? _configuration[Constants.Keys.ApplicationNameDefaultKey];
var appName = GetApplicationName(sectionKey);

var connectionString = GetConnectionString(sectionKey);

var endpoints = GetEndpoints(sectionKey);

return (appName, connectionString, endpoints, options);
}

private string GetApplicationName(string sectionKey)
{
// A known issue in previous version that the key ended with ":"
return _configuration[$"{sectionKey}:ApplicationName:"] ?? _configuration[$"{sectionKey}:ApplicationName"];
}

private string GetConnectionString(string sectionKey)
{
var connectionStringKey = $"{sectionKey}:ConnectionString";
// Fallback to ConnectionStrings:Azure:SignalR:ConnectionString format when the default one is not available
var connectionString = _configuration[Constants.Keys.ConnectionStringDefaultKey] ?? _configuration[Constants.Keys.ConnectionStringSecondaryKey];
return _configuration[connectionStringKey] ?? _configuration.GetConnectionString(connectionStringKey);
}

var endpoints = _configuration.GetEndpoints(Constants.Keys.ConnectionStringDefaultKey).ToArray();
private ServiceEndpoint[] GetEndpoints(string sectionKey)
{
var endpointKey = $"{sectionKey}:ConnectionString";
var endpoints = _configuration.GetEndpoints(endpointKey).ToArray();

if (endpoints.Length == 0)
{
endpoints = _configuration.GetEndpoints(Constants.Keys.ConnectionStringSecondaryKey).ToArray();
endpoints = _configuration.GetEndpoints($"ConnectionStrings:{endpointKey}").ToArray();
}

return (appName, connectionString, endpoints, options);
return endpoints;
}

private record class ConfigurableGracefulShutdownOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ public AppSettingsConfigScope(string setting, params string[] additionalSettings
var newSettings = additionalSettings.Select(
s =>
new KeyValuePair<string, string>(
Constants.Keys.ConnectionStringKeyPrefix + Guid.NewGuid().ToString("N")
Constants.Keys.ConnectionStringDefaultKey + ":" + Guid.NewGuid().ToString("N")
, s))
.ToList();
_originalAdditonalSettings = newSettings.Select(s =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ServiceOptionsSetupFacts
{
public const string FakeConnectionString = "Endpoint=http://fake;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0";

private static readonly string[] ConnectionStringKeyPrefixs = new string[] { Constants.Keys.ConnectionStringKeyPrefix, Constants.Keys.ConnectionStringSecondaryKeyPrefix };
private static readonly string[] ConnectionStringKeys = new[] { Constants.Keys.ConnectionStringDefaultKey, Constants.Keys.ConnectionStringSecondaryKey };

private static readonly Dictionary<string, (string, EndpointType)> EndpointDict = new Dictionary<string, (string, EndpointType)>
{
Expand All @@ -22,9 +22,9 @@ public class ServiceOptionsSetupFacts
{":secondary",(string.Empty,EndpointType.Secondary) }
};

public static IEnumerable<object[]> ParseServiceEndpointData = from section in ConnectionStringKeyPrefixs
public static IEnumerable<object[]> ParseServiceEndpointData = from section in ConnectionStringKeys
from tuple in EndpointDict
select new object[] { section + tuple.Key, tuple.Value.Item1, tuple.Value.Item2 };
select new object[] { section + ":" + tuple.Key, tuple.Value.Item1, tuple.Value.Item2 };

[Theory]
[MemberData(nameof(ParseServiceEndpointData))]
Expand Down

0 comments on commit 39e4886

Please sign in to comment.