/// <summary>
        ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
        /// </summary>
        ///
        /// <param name="host">The fully qualified host name for the Event Hubs namespace.  This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
        /// <param name="eventHubPath">The path of the specific Event Hub to connect the client to.</param>
        /// <param name="credential">The Azure managed identity credential to use for authorization.  Access controls may be specified by the Event Hubs namespace or the requeseted Event Hub, depending on Azure configuration.</param>
        /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
        ///
        public EventHubClient(string host,
                              string eventHubPath,
                              TokenCredential credential,
                              EventHubClientOptions clientOptions = default)
        {
            clientOptions = clientOptions?.Clone() ?? new EventHubClientOptions();

            Guard.ArgumentNotNullOrEmpty(nameof(host), host);
            Guard.ArgumentNotNullOrEmpty(nameof(eventHubPath), eventHubPath);
            Guard.ArgumentNotNull(nameof(credential), credential);
            ValidateClientOptions(clientOptions);

            switch (credential)
            {
            case SharedAccessSignatureCredential _:
                break;

            case EventHubSharedKeyCredential sharedKeyCredential:
                credential = sharedKeyCredential.ConvertToSharedAccessSignatureCredential(BuildResource(clientOptions.TransportType, host, eventHubPath));
                break;

            default:
                credential = new EventHubTokenCredential(credential, BuildResource(clientOptions.TransportType, host, eventHubPath));
                break;
            }

            EventHubPath  = eventHubPath;
            ClientOptions = clientOptions;
            InnerClient   = BuildTransportClient(host, eventHubPath, credential, clientOptions);
        }
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
        /// </summary>
        ///
        /// <param name="connectionString">The connection string to use for connecting to the Event Hubs namespace; it is expected that the Event Hub path and SAS token are contained in this connection string.</param>
        /// <param name="eventHubPath">The path of the specific Event Hub to connect the client to.</param>
        /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
        ///
        /// <remarks>
        ///   If the connection string is copied from the Event Hub itself, it will contain the path to the desired Event Hub,
        ///   and can be used directly without passing the <paramref name="eventHubPath" />.  The path to the Event Hub should be
        ///   passed only once, either as part of the connection string or separately.
        /// </remarks>
        ///
        public EventHubClient(string connectionString,
                              string eventHubPath,
                              EventHubClientOptions clientOptions)
        {
            clientOptions = clientOptions?.Clone() ?? new EventHubClientOptions();

            Guard.ArgumentNotNullOrEmpty(nameof(connectionString), connectionString);
            ValidateClientOptions(clientOptions);

            var connectionStringProperties = ParseConnectionString(connectionString);

            ValidateConnectionProperties(connectionStringProperties, eventHubPath, nameof(connectionString));

            var eventHubsHostName = connectionStringProperties.Endpoint.Host;

            if (String.IsNullOrEmpty(eventHubPath))
            {
                eventHubPath = connectionStringProperties.EventHubPath;
            }

            var sharedAccessSignature = new SharedAccessSignature
                                        (
                BuildResource(clientOptions.TransportType, eventHubsHostName, eventHubPath),
                connectionStringProperties.SharedAccessKeyName,
                connectionStringProperties.SharedAccessKey
                                        );

            ClientOptions = clientOptions;
            EventHubPath  = eventHubPath;
            InnerClient   = BuildTransportClient(eventHubsHostName, eventHubPath, new SharedAccessSignatureCredential(sharedAccessSignature), clientOptions);
        }
Esempio n. 3
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
        /// </summary>
        ///
        /// <param name="connectionString">The connection string to use for connecting to the Event Hubs namespace; it is expected that the Event Hub path and SAS token are contained in this connection string.</param>
        /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
        ///
        /// <remarks>
        ///   If the connection string is copied from the Event Hubs namespace, it will likely not contain the path to the desired Event Hub,
        ///   which is needed.  In this case, the path can be added manually by adding ";EntityPath=[[ EVENT HUB NAME ]]" to the end of the
        ///   connection string.  For example, ";EntityPath=telemetry-hub".
        ///
        ///   If you have defined a shared access policy directly on the Event Hub itself, then copying the connection string from that
        ///   Event Hub will result in a connection string that contains the path.
        /// </remarks>
        ///
        public EventHubClient(string connectionString,
                              EventHubClientOptions clientOptions)
        {
            Guard.ArgumentNotNullOrEmpty(nameof(connectionString), connectionString);
            ValidateClientOptions(clientOptions);

            var connectionStringProperties = ParseConnectionString(connectionString);

            ValidateConnectionStringProperties(connectionStringProperties, nameof(connectionString));

            ClientOptions = clientOptions?.Clone() ?? new EventHubClientOptions();
            EventHubPath  = connectionStringProperties.EventHubPath;
        }
Esempio n. 4
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
        /// </summary>
        ///
        /// <param name="host">The fully qualified host name for the Event Hubs namespace.  This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
        /// <param name="eventHubPath">The path of the specific Event Hub to connect the client to.</param>
        /// <param name="credential">The Azure managed identity credential to use for authorization.  Access controls may be specified by the Event Hubs namespace or the requeseted Event Hub, depending on Azure configuration.</param>
        /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
        ///
        public EventHubClient(string host,
                              string eventHubPath,
                              TokenCredential credential,
                              EventHubClientOptions clientOptions = default)
        {
            Guard.ArgumentNotNull(nameof(host), host);
            Guard.ArgumentNotNullOrEmpty(nameof(eventHubPath), eventHubPath);
            Guard.ArgumentNotNull(nameof(credential), credential);

            EventHubPath  = eventHubPath;
            Credential    = credential;
            ClientOptions = clientOptions;
        }
Esempio n. 5
0
        /// <summary>
        ///   Builds an Event Hub Client specific to the protocol and transport specified by the
        ///   requested connection type of the <paramref name="options" />.
        /// </summary>
        ///
        /// <param name="host">The fully qualified host name for the Event Hubs namespace.</param>
        /// <param name="eventHubPath">The path to a specific Event Hub.</param>
        /// <param name="credential">The Azure managed identity credential to use for authorization.</param>
        /// <param name="options">The set of options to use for the client.</param>
        ///
        /// <returns>A client generalization spcecific to the specified protocol/transport to which operations may be delegated.</returns>
        ///
        /// <remarks>
        ///   As an internal method, only basic sanity checks are performed against arguments.  It is
        ///   assumed that callers are trusted and have performed deep validation.
        ///
        ///   Parameters passed are also assumed to be owned by thee transport client and safe to mutate or dispose;
        ///   creation of clones or otherwise protecting the parameters is assumed to be the purview of the caller.
        /// </remarks>
        ///
        internal virtual TransportEventHubClient BuildTransportClient(string host,
                                                                      string eventHubPath,
                                                                      TokenCredential credential,
                                                                      EventHubClientOptions options)
        {
            switch (options.TransportType)
            {
            case TransportType.AmqpTcp:
            case TransportType.AmqpWebSockets:
                return(new TrackOneEventHubClient(host, eventHubPath, credential, options));

            default:
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidTransportType, options.TransportType.ToString()), nameof(options.TransportType));
            }
        }
Esempio n. 6
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
        /// </summary>
        ///
        /// <param name="host">The fully qualified host name for the Event Hubs namespace.  This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
        /// <param name="eventHubPath">The path of the specific Event Hub to connect the client to.</param>
        /// <param name="credential">The Azure managed identity credential to use for authorization.  Access controls may be specified by the Event Hubs namespace or the requeseted Event Hub, depending on Azure configuration.</param>
        /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
        ///
        public EventHubClient(string host,
                              string eventHubPath,
                              TokenCredential credential,
                              EventHubClientOptions clientOptions = default)
        {
            Guard.ArgumentNotNullOrEmpty(nameof(host), host);
            Guard.ArgumentNotNullOrEmpty(nameof(eventHubPath), eventHubPath);
            Guard.ArgumentNotNull(nameof(credential), credential);
            ValidateClientOptions(clientOptions);

            EventHubPath  = eventHubPath;
            Credential    = credential;
            ClientOptions = clientOptions?.Clone() ?? new EventHubClientOptions();
            InnerClient   = BuildTransportClient(host, eventHubPath, credential, ClientOptions);
        }
Esempio n. 7
0
        /// <summary>
        ///   Performs the actions needed to validate the <see cref="EventHubClientOptions" /> associated
        ///   with this client.
        /// </summary>
        ///
        /// <param name="clientOptions">The set of options to validate.</param>
        ///
        /// <remarks>
        ///   In the case that the options violate an invariant or otherwise represent a combination that
        ///   is not permissible, an appropriate exception will be thrown.
        /// </remarks>
        ///
        private static void ValidateClientOptions(EventHubClientOptions clientOptions)
        {
            // If there were no options passed, they cannot be in an invalid state.

            if (clientOptions == null)
            {
                return;
            }

            // A proxy is only valid when websockets is used as the transport.

            if ((clientOptions.TransportType == TransportType.AmqpTcp) && (clientOptions.Proxy != null))
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.ProxyMustUseWebsockets), nameof(clientOptions));
            }
        }
Esempio n. 8
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
 /// </summary>
 ///
 /// <param name="connectionString">The connection string to use for connecting to the Event Hubs namespace; it is expected that the Event Hub path and SAS token are contained in this connection string.</param>
 /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
 ///
 /// <remarks>
 ///   If the connection string is copied from the Event Hubs namespace, it will likely not contain the path to the desired Event Hub,
 ///   which is needed.  In this case, the path can be added manually by adding ";EntityPath=[[ EVENT HUB NAME ]]" to the end of the
 ///   connection string.  For example, ";EntityPath=telemetry-hub".
 ///
 ///   If you have defined a shared access policy directly on the Event Hub itself, then copying the connection string from that
 ///   Event Hub will result in a connection string that contains the path.
 /// </remarks>
 ///
 public EventHubClient(string connectionString,
                       EventHubClientOptions clientOptions) : this(connectionString, null, clientOptions)
 {
 }
Esempio n. 9
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
 /// </summary>
 ///
 /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
 ///
 /// <remarks>
 ///   Because this is a non-public constructor, it is assumed that the <paramref name="clientOptions" /> passed are
 ///   owned by this instance and are safe from changes made by consumers.  It is considered the responsibility of the
 ///   caller to ensure that any needed cloning of options is performed.
 /// </remarks>
 ///
 protected EventHubClient(EventHubClientOptions clientOptions)
 {
     ValidateClientOptions(clientOptions);
     ClientOptions = clientOptions;
 }
Esempio n. 10
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="EventHubClient"/> class.
 /// </summary>
 ///
 /// <param name="connectionString">The connection string to use for connecting to the Event Hubs namespace; it is expected that the Event Hub path and SAS token are contained in this connection string.</param>
 /// <param name="clientOptions">A set of options to apply when configuring the client.</param>
 ///
 /// <remarks>
 ///   If the connection string is copied from the Event Hubs namespace, it will likely not contain the path to the desired Event Hub,
 ///   which is needed.  In this case, the path can be added manually by adding ";EntityPath=[[ EVENT HUB NAME ]]" to the end of the
 ///   connection string.  For example, ";EntityPath=telemetry-hub".
 ///
 ///   If you have defined a shared access policy directly on the Event Hub itself, then copying the connection string from that
 ///   Event Hub will result in a connection string that contains the path.
 /// </remarks>
 ///
 public EventHubClient(string connectionString,
                       EventHubClientOptions clientOptions)
 {
 }