-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathConnectionStringParser.cs
More file actions
111 lines (91 loc) · 4.15 KB
/
ConnectionStringParser.cs
File metadata and controls
111 lines (91 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
namespace ServiceControl.Transports.ASBS
{
using System;
using System.Data.Common;
using Azure.Messaging.ServiceBus;
public static class ConnectionStringParser
{
public static ConnectionSettings Parse(string connectionString)
{
if (!connectionString.Contains("="))
{
if (connectionString.Contains("sb://"))
{
throw new Exception("When using a fully-qualified namespace the 'sb://' prefix is not allowed");
}
if (connectionString.EndsWith("/"))
{
throw new Exception("When using a fully-qualified namespace a trailing '/' is not allowed");
}
return new ConnectionSettings(new TokenCredentialAuthentication(connectionString));
}
TimeSpan? queryDelayInterval = null;
string topicNameString = null;
var useWebSockets = false;
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };
if (builder.TryGetValue("QueueLengthQueryDelayInterval", out var value))
{
if (!int.TryParse(value.ToString(), out var delayInterval))
{
throw new Exception($"Can't parse {value} as a valid query delay interval.");
}
queryDelayInterval = TimeSpan.FromMilliseconds(delayInterval);
}
if (builder.TryGetValue("TopicName", out var topicName))
{
topicNameString = (string)topicName;
}
if (builder.TryGetValue("TransportType", out var transportTypeString) && Enum.TryParse((string)transportTypeString, true, out ServiceBusTransportType transportType) && transportType == ServiceBusTransportType.AmqpWebSockets)
{
useWebSockets = true;
}
var hasEndpoint = builder.TryGetValue("Endpoint", out var endpoint);
if (!hasEndpoint)
{
throw new Exception("The Endpoint property is mandatory on the connection string");
}
string clientIdString = null;
if (builder.TryGetValue("ClientId", out var clientId))
{
clientIdString = (string)clientId;
}
var enablePartitioning = false;
if (builder.TryGetValue("EnablePartitioning", out var enablePartitioningString))
{
if (!bool.TryParse((string)enablePartitioningString, out enablePartitioning))
{
throw new Exception(
$"Cannot enable partitioning, the specified value '{enablePartitioningString}' cannot be converted to a bool.");
}
}
string hierarchyNamespace = null;
if (builder.TryGetValue("HierarchyNamespace", out var hierarchyNamespaceString))
{
hierarchyNamespace = (string)hierarchyNamespaceString;
}
var shouldUseManagedIdentity = builder.TryGetValue("Authentication", out var authType) && (string)authType == "Managed Identity";
if (shouldUseManagedIdentity)
{
var fullyQualifiedNamespace = endpoint.ToString().TrimEnd('/').Replace("sb://", "");
return new ConnectionSettings(
new TokenCredentialAuthentication(fullyQualifiedNamespace, clientIdString),
topicNameString,
useWebSockets,
enablePartitioning,
queryDelayInterval,
hierarchyNamespace);
}
if (clientIdString != null)
{
throw new Exception("ClientId is only allowed when using Managed Identity (Authentication Type=Managed Identity)");
}
return new ConnectionSettings(
new SharedAccessSignatureAuthentication(connectionString),
topicNameString,
useWebSockets,
enablePartitioning,
queryDelayInterval,
hierarchyNamespace);
}
}
}