| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | |
| | | 6 | | namespace Azure.Messaging.EventHubs.Core |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// The set of properties that comprise a connection string from the |
| | | 10 | | /// Azure portal. |
| | | 11 | | /// </summary> |
| | | 12 | | /// |
| | | 13 | | internal struct ConnectionStringProperties |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// The endpoint to be used for connecting to the Event Hubs namespace. |
| | | 17 | | /// </summary> |
| | | 18 | | /// |
| | | 19 | | /// <value>The endpoint address, including protocol, from the connection string.</value> |
| | | 20 | | /// |
| | 0 | 21 | | public Uri Endpoint { get; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The name of the specific Event Hub instance under the associated Event Hubs namespace. |
| | | 25 | | /// </summary> |
| | | 26 | | /// |
| | 0 | 27 | | public string EventHubName { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The name of the shared access key, either for the Event Hubs namespace |
| | | 31 | | /// or the Event Hub. |
| | | 32 | | /// </summary> |
| | | 33 | | /// |
| | 0 | 34 | | public string SharedAccessKeyName { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The value of the shared access key, either for the Event Hubs namespace |
| | | 38 | | /// or the Event Hub. |
| | | 39 | | /// </summary> |
| | | 40 | | /// |
| | 0 | 41 | | public string SharedAccessKey { get; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="ConnectionStringProperties"/> structure. |
| | | 45 | | /// </summary> |
| | | 46 | | /// |
| | | 47 | | /// <param name="endpoint">The endpoint of the Event Hubs namespace.</param> |
| | | 48 | | /// <param name="eventHubName">The name of the specific Event Hub under the namespace.</param> |
| | | 49 | | /// <param name="sharedAccessKeyName">The name of the shared access key, to use authorization.</param> |
| | | 50 | | /// <param name="sharedAccessKey">The shared access key to use for authorization.</param> |
| | | 51 | | /// |
| | | 52 | | public ConnectionStringProperties(Uri endpoint, |
| | | 53 | | string eventHubName, |
| | | 54 | | string sharedAccessKeyName, |
| | | 55 | | string sharedAccessKey) |
| | | 56 | | { |
| | 0 | 57 | | Endpoint = endpoint; |
| | 0 | 58 | | EventHubName = eventHubName; |
| | 0 | 59 | | SharedAccessKeyName = sharedAccessKeyName; |
| | 0 | 60 | | SharedAccessKey = sharedAccessKey; |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Performs the actions needed to validate the set of connection string properties for connecting to the |
| | | 65 | | /// Event Hubs service. |
| | | 66 | | /// </summary> |
| | | 67 | | /// |
| | | 68 | | /// <param name="explicitEventHubName">The name of the Event Hub that was explicitly passed independent of the c |
| | | 69 | | /// <param name="connectionStringArgumentName">The name of the argument associated with the connection string; t |
| | | 70 | | /// |
| | | 71 | | /// <exception cref="ArgumentException">In the case that the properties violate an invariant or otherwise repres |
| | | 72 | | /// |
| | | 73 | | public void Validate(string explicitEventHubName, |
| | | 74 | | string connectionStringArgumentName) |
| | | 75 | | { |
| | | 76 | | // The Event Hub name may only be specified in one of the possible forms, either as part of the |
| | | 77 | | // connection string or as a stand-alone parameter, but not both. If specified in both to the same |
| | | 78 | | // value, then do not consider this a failure. |
| | | 79 | | |
| | 0 | 80 | | if ((!string.IsNullOrEmpty(explicitEventHubName)) |
| | 0 | 81 | | && (!string.IsNullOrEmpty(EventHubName)) |
| | 0 | 82 | | && (!string.Equals(explicitEventHubName, EventHubName, StringComparison.InvariantCultureIgnoreCase))) |
| | | 83 | | { |
| | 0 | 84 | | throw new ArgumentException(Resources.OnlyOneEventHubNameMayBeSpecified, connectionStringArgumentName); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // Ensure that each of the needed components are present for connecting. |
| | | 88 | | |
| | 0 | 89 | | if ((string.IsNullOrEmpty(explicitEventHubName)) && (string.IsNullOrEmpty(EventHubName)) |
| | 0 | 90 | | || (string.IsNullOrEmpty(Endpoint?.Host)) |
| | 0 | 91 | | || (string.IsNullOrEmpty(SharedAccessKeyName)) |
| | 0 | 92 | | || (string.IsNullOrEmpty(SharedAccessKey))) |
| | | 93 | | { |
| | 0 | 94 | | throw new ArgumentException(Resources.MissingConnectionInformation, connectionStringArgumentName); |
| | | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |