Skip to content

Commit f317cd0

Browse files
committed
Clean up EventHub connection string code
Follow up from dotnet#7453
1 parent 9e52dff commit f317cd0

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/Components/Aspire.Azure.Messaging.EventHubs/AzureMessagingEventHubsSettings.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,20 @@ private static bool GetTracingDefaultValue()
8282
return false;
8383
}
8484

85+
/// <summary>
86+
/// Extracts properties from the connection string.
87+
/// </summary>
88+
/// <remarks>
89+
/// The connection string can be in the following formats:
90+
/// A valid FullyQualifiedNamespace
91+
/// Endpoint={valid FullyQualifiedNamespace} - optionally with [;EntityPath={hubName}[;ConsumerGroup={groupName}]]
92+
/// {valid EventHub ConnectionString} - optionally with [;ConsumerGroup={groupName}]
93+
/// </remarks>
8594
void IConnectionStringSettings.ParseConnectionString(string? connectionString)
8695
{
8796
if (!string.IsNullOrEmpty(connectionString))
8897
{
89-
// a event hubs namespace can't contain ';'. if it is found assume it is a connection string
98+
// an event hubs namespace can't contain ';'. if it is found assume it is a connection string
9099
if (!connectionString.Contains(';'))
91100
{
92101
FullyQualifiedNamespace = connectionString;
@@ -98,36 +107,34 @@ void IConnectionStringSettings.ParseConnectionString(string? connectionString)
98107
ConnectionString = connectionString
99108
};
100109

110+
// Note: Strip out the ConsumerGroup and EntityPath from the connection string in order
111+
// to tell if we are left with just Endpoint.
112+
101113
if (connectionBuilder.TryGetValue("ConsumerGroup", out var group))
102114
{
103115
SetConsumerGroup(group.ToString());
104116

105-
// remove ConsumerGroup from the connection builder since it isn't a connection property
106-
// in regular Event Hubs connection strings
107117
connectionBuilder.Remove("ConsumerGroup");
108118
}
109119

110-
var hasEntityPath = connectionBuilder.TryGetValue("EntityPath", out var entityPath);
111-
if (hasEntityPath)
120+
if (connectionBuilder.TryGetValue("EntityPath", out var entityPath))
112121
{
113-
// don't strip off EntityPath from the connection string because
114-
// it is a valid connection property in regular Event Hubs connection strings
115122
EventHubName = entityPath!.ToString();
123+
124+
connectionBuilder.Remove("EntityPath");
116125
}
117126

118-
if (connectionBuilder.Count == 1 ||
119-
(connectionBuilder.Count == 2 && hasEntityPath))
127+
if (connectionBuilder.Count == 1 &&
128+
connectionBuilder.TryGetValue("Endpoint", out var endpoint))
120129
{
121-
if (connectionBuilder.TryGetValue("Endpoint", out var endpoint))
122-
{
123-
// if all that's left is Endpoint or Endpoint+EntityPath,
124-
// it is a fully qualified namespace
125-
FullyQualifiedNamespace = endpoint.ToString();
126-
return;
127-
}
130+
// if all that's left is Endpoint, it is a fully qualified namespace
131+
FullyQualifiedNamespace = endpoint.ToString();
132+
return;
128133
}
129134

130135
// if we got here, it's a full connection string
136+
// use the original connection string since connectionBuilder was modified above.
137+
// Extra keys, like ConsumerGroup, are ignored by the EventHub client constructors.
131138
ConnectionString = connectionString;
132139
}
133140
}

0 commit comments

Comments
 (0)