< Summary

Class:Microsoft.Azure.EventHubs.Processor.EventProcessorHost
Assembly:Microsoft.Azure.EventHubs.Processor
File(s):C:\Git\azure-sdk-for-net\sdk\eventhub\Microsoft.Azure.EventHubs.Processor\src\EventProcessorHost.cs
Covered lines:0
Uncovered lines:181
Coverable lines:181
Total lines:530
Line coverage:0% (0 of 181)
Covered branches:0
Total branches:22
Branch coverage:0% (0 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
.ctor(...)-0%100%
.ctor(...)-0%0%
.ctor(...)-0%100%
.ctor(...)-0%100%
.ctor(...)-0%100%
.ctor(...)-0%0%
.ctor(...)-0%100%
.ctor(...)-0%100%
get_HostName()-0%100%
get_EventHubPath()-0%100%
get_ConsumerGroupName()-0%100%
get_EndpointAddress()-0%100%
get_TransportType()-0%100%
get_OperationTimeout()-0%100%
get_PartitionManagerOptions()-0%100%
get_CheckpointManager()-0%100%
get_EventProcessorOptions()-0%100%
get_LeaseManager()-0%100%
get_ProcessorFactory()-0%100%
get_PartitionManager()-0%100%
RegisterEventProcessorAsync()-0%100%
RegisterEventProcessorAsync(...)-0%100%
RegisterEventProcessorFactoryAsync(...)-0%100%
RegisterEventProcessorFactoryAsync()-0%0%
UnregisterEventProcessorAsync()-0%100%
CreateHostName(...)-0%0%
CreateEventHubClient()-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventhub\Microsoft.Azure.EventHubs.Processor\src\EventProcessorHost.cs

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.EventHubs.Processor
 5{
 6    using System;
 7    using System.Threading.Tasks;
 8    using Microsoft.Azure.EventHubs.Primitives;
 9    using Microsoft.Azure.Storage;
 10
 11    /// <summary>
 12    /// Represents a host for processing Event Hubs event data.
 13    /// </summary>
 14    public sealed class EventProcessorHost
 15    {
 16        // A processor host will work on either the token provider or the connection string.
 17        readonly ITokenProvider tokenProvider;
 18        string eventHubConnectionString;
 19
 20        /// <summary>
 21        /// Create a new host to process events from an Event Hub.
 22        ///
 23        /// <para>Since Event Hubs are frequently used for scale-out, high-traffic scenarios, generally there will
 24        /// be only one host per process, and the processes will be run on separate machines. However, it is
 25        /// supported to run multiple hosts on one machine, or even within one process, if throughput is not
 26        /// a concern.</para>
 27        ///
 28        /// This overload of the constructor uses the default, built-in lease and checkpoint managers. The
 29        /// Azure Storage account specified by the storageConnectionString parameter is used by the built-in
 30        /// managers to record leases and checkpoints.
 31        /// </summary>
 32        /// <param name="eventHubPath">The name of the EventHub.</param>
 33        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 34        /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param>
 35        /// <param name="storageConnectionString">Connection string to Azure Storage account used for leases and checkpo
 36        /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag
 37        public EventProcessorHost(
 38            string eventHubPath,
 39            string consumerGroupName,
 40            string eventHubConnectionString,
 41            string storageConnectionString,
 42            string leaseContainerName)
 043            : this(EventProcessorHost.CreateHostName(null),
 044                eventHubPath,
 045                consumerGroupName,
 046                eventHubConnectionString,
 047                storageConnectionString,
 048                leaseContainerName,
 049                null)
 50        {
 051        }
 52
 53        /// <summary>
 54        /// Create a new host to process events from an Event Hub.
 55        ///
 56        /// <para>This overload of the constructor uses the default, built-in lease and checkpoint managers.</para>
 57        /// </summary>
 58        /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en
 59        /// <param name="eventHubPath">The name of the EventHub.</param>
 60        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 61        /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param>
 62        /// <param name="storageConnectionString">Connection string to Azure Storage account used for leases and checkpo
 63        /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag
 64        /// <param name="storageBlobPrefix">Prefix used when naming blobs within the storage container.</param>
 65        public EventProcessorHost(
 66            string hostName,
 67            string eventHubPath,
 68            string consumerGroupName,
 69            string eventHubConnectionString,
 70            string storageConnectionString,
 71            string leaseContainerName,
 72            string storageBlobPrefix = null)
 073            : this(hostName,
 074                eventHubPath,
 075                consumerGroupName,
 076                eventHubConnectionString,
 077                new AzureStorageCheckpointLeaseManager(storageConnectionString, leaseContainerName, storageBlobPrefix))
 78        {
 079        }
 80
 81        /// <summary>
 82        /// Create a new host to process events from an Event Hub.
 83        ///
 84        /// <para>This overload of the constructor allows maximum flexibility.
 85        /// This one allows the caller to specify the name of the processor host as well.
 86        /// The overload also allows the caller to provide their own lease and checkpoint managers to replace the built-
 87        /// ones based on Azure Storage.</para>
 88        /// </summary>
 89        /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en
 90        /// <param name="eventHubPath">The name of the EventHub.</param>
 91        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 92        /// <param name="eventHubConnectionString">Connection string for the Event Hub to receive from.</param>
 93        /// <param name="checkpointManager">Object implementing ICheckpointManager which handles partition checkpointing
 94        /// <param name="leaseManager">Object implementing ILeaseManager which handles leases for partitions.</param>
 095        public EventProcessorHost(
 096             string hostName,
 097             string eventHubPath,
 098             string consumerGroupName,
 099             string eventHubConnectionString,
 0100             ICheckpointManager checkpointManager,
 0101             ILeaseManager leaseManager)
 102        {
 0103            Guard.ArgumentNotNullOrWhiteSpace(nameof(hostName), hostName);
 0104            Guard.ArgumentNotNullOrWhiteSpace(nameof(consumerGroupName), consumerGroupName);
 0105            Guard.ArgumentNotNull(nameof(checkpointManager), checkpointManager);
 0106            Guard.ArgumentNotNull(nameof(leaseManager), leaseManager);
 107
 0108            var csb = new EventHubsConnectionStringBuilder(eventHubConnectionString);
 0109            if (string.IsNullOrEmpty(eventHubPath))
 110            {
 111                // Entity path is expected in the connection string if not provided with eventHubPath parameter.
 0112                if (string.IsNullOrEmpty(csb.EntityPath))
 113                {
 0114                    throw new ArgumentException(nameof(eventHubConnectionString),
 0115                        "Provide EventHub entity path either in eventHubPath parameter or in eventHubConnectionString.")
 116                }
 117            }
 118            else
 119            {
 120                // Entity path should not conflict with connection string.
 0121                if (!string.IsNullOrEmpty(csb.EntityPath) &&
 0122                    string.Compare(csb.EntityPath, eventHubPath, StringComparison.OrdinalIgnoreCase) != 0)
 123                {
 0124                    throw new ArgumentException(nameof(eventHubConnectionString),
 0125                        "Provided EventHub path in eventHubPath parameter conflicts with the path in provided EventHubs 
 126                }
 127
 0128                csb.EntityPath = eventHubPath;
 129            }
 130
 0131            this.HostName = hostName;
 0132            this.EventHubPath = csb.EntityPath;
 0133            this.ConsumerGroupName = consumerGroupName;
 0134            this.eventHubConnectionString = csb.ToString();
 0135            this.CheckpointManager = checkpointManager;
 0136            this.LeaseManager = leaseManager;
 0137            this.TransportType = csb.TransportType;
 0138            this.OperationTimeout = csb.OperationTimeout;
 0139            this.EndpointAddress = csb.Endpoint;
 0140            this.PartitionManager = new PartitionManager(this);
 0141            ProcessorEventSource.Log.EventProcessorHostCreated(this.HostName, this.EventHubPath);
 0142        }
 143
 144        /// <summary>
 145        /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/>
 146        /// </summary>
 147        /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi
 148        /// <param name="eventHubPath">The name of the EventHub.</param>
 149        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 150        /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param>
 151        /// <param name="cloudStorageAccount">Azure Storage account used for leases and checkpointing.</param>
 152        /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag
 153        public EventProcessorHost(
 154            Uri endpointAddress,
 155            string eventHubPath,
 156            string consumerGroupName,
 157            ITokenProvider tokenProvider,
 158            CloudStorageAccount cloudStorageAccount,
 159            string leaseContainerName)
 0160            : this(EventProcessorHost.CreateHostName(null),
 0161                  endpointAddress,
 0162                  eventHubPath,
 0163                  consumerGroupName,
 0164                  tokenProvider,
 0165                  cloudStorageAccount,
 0166                  leaseContainerName)
 167        {
 0168        }
 169
 170        /// <summary>
 171        /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/>
 172        /// </summary>
 173        /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en
 174        /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi
 175        /// <param name="eventHubPath">The name of the EventHub.</param>
 176        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 177        /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param>
 178        /// <param name="cloudStorageAccount">Azure Storage account used for leases and checkpointing.</param>
 179        /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag
 180        /// <param name="storageBlobPrefix">Prefix used when naming blobs within the storage container.</param>
 181        /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param>
 182        /// <param name="transportType">Transport type on connection.</param>
 183        public EventProcessorHost(
 184            string hostName,
 185            Uri endpointAddress,
 186            string eventHubPath,
 187            string consumerGroupName,
 188            ITokenProvider tokenProvider,
 189            CloudStorageAccount cloudStorageAccount,
 190            string leaseContainerName,
 191            string storageBlobPrefix = null,
 192            TimeSpan? operationTimeout = null,
 193            TransportType transportType = TransportType.Amqp)
 0194            : this(hostName,
 0195                  endpointAddress,
 0196                  eventHubPath,
 0197                  consumerGroupName,
 0198                  tokenProvider,
 0199                  new AzureStorageCheckpointLeaseManager(cloudStorageAccount, leaseContainerName, storageBlobPrefix),
 0200                  operationTimeout,
 0201                  transportType)
 202        {
 0203        }
 204
 205        /// <summary>
 206        /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/>
 207        /// </summary>
 208        /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi
 209        /// <param name="eventHubPath">The name of the EventHub.</param>
 210        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 211        /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param>
 212        /// <param name="cloudStorageAccount">Azure Storage account used for leases and checkpointing.</param>
 213        /// <param name="leaseContainerName">Azure Storage container name for use by built-in lease and checkpoint manag
 214        /// <param name="storageBlobPrefix">Prefix used when naming blobs within the storage container.</param>
 215        /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param>
 216        /// <param name="transportType">Transport type on connection.</param>
 217        public EventProcessorHost(
 218            Uri endpointAddress,
 219            string eventHubPath,
 220            string consumerGroupName,
 221            ITokenProvider tokenProvider,
 222            CloudStorageAccount cloudStorageAccount,
 223            string leaseContainerName,
 224            string storageBlobPrefix = null,
 225            TimeSpan? operationTimeout = null,
 226            TransportType transportType = TransportType.Amqp)
 0227            : this(EventProcessorHost.CreateHostName(null),
 0228                endpointAddress,
 0229                eventHubPath,
 0230                consumerGroupName,
 0231                tokenProvider,
 0232                cloudStorageAccount,
 0233                leaseContainerName,
 0234                storageBlobPrefix,
 0235                operationTimeout,
 0236                transportType)
 237        {
 0238        }
 239
 240        /// <summary>
 241        /// Create a new host to process events from an Event Hub with provided <see cref="TokenProvider"/>
 242        /// </summary>
 243        /// <param name="hostName">Name of the processor host. MUST BE UNIQUE. Strongly recommend including a Guid to en
 244        /// <param name="endpointAddress">Fully qualified domain name for Event Hubs. Most likely, {yournamespace}.servi
 245        /// <param name="eventHubPath">The name of the EventHub.</param>
 246        /// <param name="consumerGroupName">The name of the consumer group within the Event Hub.</param>
 247        /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param>
 248        /// <param name="checkpointManager">Object implementing ICheckpointManager which handles partition checkpointing
 249        /// <param name="leaseManager">Object implementing ILeaseManager which handles leases for partitions.</param>
 250        /// <param name="operationTimeout">Operation timeout for Event Hubs operations.</param>
 251        /// <param name="transportType">Transport type on connection.</param>
 0252        public EventProcessorHost(
 0253            string hostName,
 0254            Uri endpointAddress,
 0255            string eventHubPath,
 0256            string consumerGroupName,
 0257            ITokenProvider tokenProvider,
 0258            ICheckpointManager checkpointManager,
 0259            ILeaseManager leaseManager,
 0260            TimeSpan? operationTimeout = null,
 0261            TransportType transportType = TransportType.Amqp)
 262        {
 0263            Guard.ArgumentNotNullOrWhiteSpace(nameof(hostName), hostName);
 0264            Guard.ArgumentNotNull(nameof(endpointAddress), endpointAddress);
 0265            Guard.ArgumentNotNullOrWhiteSpace(nameof(eventHubPath), eventHubPath);
 0266            Guard.ArgumentNotNullOrWhiteSpace(nameof(consumerGroupName), consumerGroupName);
 0267            Guard.ArgumentNotNull(nameof(tokenProvider), tokenProvider);
 0268            Guard.ArgumentNotNull(nameof(checkpointManager), checkpointManager);
 0269            Guard.ArgumentNotNull(nameof(leaseManager), leaseManager);
 270
 0271            this.HostName = hostName;
 0272            this.EndpointAddress = endpointAddress;
 0273            this.EventHubPath = eventHubPath;
 0274            this.ConsumerGroupName = consumerGroupName;
 0275            this.tokenProvider = tokenProvider;
 0276            this.CheckpointManager = checkpointManager;
 0277            this.LeaseManager = leaseManager;
 0278            this.TransportType = transportType;
 0279            this.OperationTimeout = operationTimeout ?? ClientConstants.DefaultOperationTimeout;
 0280            this.PartitionManager = new PartitionManager(this);
 0281            ProcessorEventSource.Log.EventProcessorHostCreated(this.HostName, this.EventHubPath);
 0282        }
 283
 284        // Using this intermediate constructor to create single combined manager to be used as
 285        // both lease manager and checkpoint manager.
 286        EventProcessorHost(
 287                string hostName,
 288                string eventHubPath,
 289                string consumerGroupName,
 290                string eventHubConnectionString,
 291                AzureStorageCheckpointLeaseManager combinedManager)
 0292            : this(hostName,
 0293                  eventHubPath,
 0294                  consumerGroupName,
 0295                  eventHubConnectionString,
 0296                  combinedManager,
 0297                  combinedManager)
 298        {
 0299        }
 300
 301        // Using this intermediate constructor to create single combined manager to be used as
 302        // both lease manager and checkpoint manager.
 303        EventProcessorHost(
 304            string hostName,
 305            Uri endpointAddress,
 306            string eventHubPath,
 307            string consumerGroupName,
 308            ITokenProvider tokenProvider,
 309            AzureStorageCheckpointLeaseManager combinedManager,
 310            TimeSpan? operationTimeout = null,
 311            TransportType transportType = TransportType.Amqp)
 0312            : this(hostName,
 0313                endpointAddress,
 0314                eventHubPath,
 0315                consumerGroupName,
 0316                tokenProvider,
 0317                combinedManager,
 0318                combinedManager,
 0319                operationTimeout,
 0320                transportType)
 321        {
 0322        }
 323
 324        /// <summary>
 325        /// Returns processor host name.
 326        /// If the processor host name was automatically generated, this is the only way to get it.
 327        /// </summary>
 0328        public string HostName { get; }
 329
 330        /// <summary>
 331        /// Gets the event hub path.
 332        /// </summary>
 0333        public string EventHubPath { get; }
 334
 335        /// <summary>
 336        /// Gets the consumer group name.
 337        /// </summary>
 0338        public string ConsumerGroupName { get; }
 339
 340        /// <summary>
 341        /// Gets the event endpoint URI.
 342        /// </summary>
 0343        public Uri EndpointAddress { get; }
 344
 345        /// <summary>
 346        /// Gets the transport type.
 347        /// </summary>
 0348        public TransportType TransportType { get; }
 349
 350        /// <summary>
 351        /// Gets the operation timeout.
 352        /// </summary>
 0353        public TimeSpan OperationTimeout { get; internal set; }
 354
 355        /// <summary>Gets or sets the
 356        /// <see cref="PartitionManagerOptions" /> instance used by the
 357        /// <see cref="EventProcessorHost" /> object.</summary>
 358        /// <value>The <see cref="PartitionManagerOptions" /> instance.</value>
 0359        public PartitionManagerOptions PartitionManagerOptions { get; set; }
 360
 361        // All of these accessors are for internal use only.
 0362        internal ICheckpointManager CheckpointManager { get; }
 363
 0364        internal EventProcessorOptions EventProcessorOptions { get; private set; }
 365
 0366        internal ILeaseManager LeaseManager { get; private set; }
 367
 0368        internal IEventProcessorFactory ProcessorFactory { get; private set; }
 369
 0370        internal PartitionManager PartitionManager { get; private set; }
 371
 372        /// <summary>
 373        /// This registers <see cref="IEventProcessor"/> implementation with the host using <see cref="DefaultEventProce
 374        /// This also starts the host and causes it to start participating in the partition distribution process.
 375        /// </summary>
 376        /// <typeparam name="T">Implementation of your application specific <see cref="IEventProcessor"/>.</typeparam>
 377        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
 378        public Task RegisterEventProcessorAsync<T>() where T : IEventProcessor, new()
 379        {
 0380            return RegisterEventProcessorAsync<T>(EventProcessorOptions.DefaultOptions);
 381        }
 382
 383        /// <summary>
 384        /// This registers <see cref="IEventProcessor"/> implementation with the host using <see cref="DefaultEventProce
 385        /// This also starts the host and causes it to start participating in the partition distribution process.
 386        /// </summary>
 387        /// <typeparam name="T">Implementation of your application specific <see cref="IEventProcessor"/>.</typeparam>
 388        /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pum
 389        /// is acquired for a particular partition of EventHub.</param>
 390        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
 391        public Task RegisterEventProcessorAsync<T>(EventProcessorOptions processorOptions) where T : IEventProcessor, ne
 392        {
 0393            IEventProcessorFactory f = new DefaultEventProcessorFactory<T>();
 0394            return RegisterEventProcessorFactoryAsync(f, processorOptions);
 395        }
 396
 397        /// <summary>
 398        /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an 
 399        /// <see cref="IEventProcessor"/> when it takes ownership of a partition.  This also starts the host and causes 
 400        /// in the partition distribution process.
 401        /// </summary>
 402        /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param>
 403        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
 404        public Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory)
 405        {
 0406            var epo = EventProcessorOptions.DefaultOptions;
 0407            epo.ReceiveTimeout = TimeSpan.MinValue;
 0408            return RegisterEventProcessorFactoryAsync(factory, epo);
 409        }
 410
 411        /// <summary>
 412        /// This registers <see cref="IEventProcessorFactory"/> implementation with the host which is used to create an 
 413        /// <see cref="IEventProcessor"/> when it takes ownership of a partition.  This also starts the host and causes 
 414        /// in the partition distribution process.
 415        /// </summary>
 416        /// <param name="factory">Instance of <see cref="IEventProcessorFactory"/> implementation.</param>
 417        /// <param name="processorOptions"><see cref="EventProcessorOptions"/> to control various aspects of message pum
 418        /// is acquired for a particular partition of EventHub.</param>
 419        /// <returns>A task to indicate EventProcessorHost instance is started.</returns>
 420        public async Task RegisterEventProcessorFactoryAsync(IEventProcessorFactory factory, EventProcessorOptions proce
 421        {
 0422            Guard.ArgumentNotNull(nameof(factory), factory);
 0423            Guard.ArgumentNotNull(nameof(processorOptions), processorOptions);
 424
 425            // Initialize partition manager options with default values if not already set by the client.
 0426            if (this.PartitionManagerOptions == null)
 427            {
 428                // Assign partition manager with default options.
 0429                this.PartitionManagerOptions = new PartitionManagerOptions();
 430            }
 431
 0432            ProcessorEventSource.Log.EventProcessorHostOpenStart(this.HostName, factory.GetType().ToString());
 433
 434            try
 435            {
 436                // Override operation timeout by receive timeout?
 0437                if (processorOptions.ReceiveTimeout > TimeSpan.MinValue)
 438                {
 0439                    this.OperationTimeout = processorOptions.ReceiveTimeout;
 440
 0441                    if (this.eventHubConnectionString != null)
 442                    {
 0443                        var cbs = new EventHubsConnectionStringBuilder(this.eventHubConnectionString)
 0444                        {
 0445                            OperationTimeout = processorOptions.ReceiveTimeout
 0446                        };
 0447                        this.eventHubConnectionString = cbs.ToString();
 448                    }
 449                }
 450
 451                // Initialize lease manager if this is an AzureStorageCheckpointLeaseManager
 0452                (this.LeaseManager as AzureStorageCheckpointLeaseManager)?.Initialize(this);
 453
 0454                this.ProcessorFactory = factory;
 0455                this.EventProcessorOptions = processorOptions;
 0456                await this.PartitionManager.StartAsync().ConfigureAwait(false);
 0457            }
 0458            catch (Exception e)
 459            {
 0460                ProcessorEventSource.Log.EventProcessorHostOpenError(this.HostName, e.ToString());
 0461                throw;
 462            }
 463            finally
 464            {
 0465                ProcessorEventSource.Log.EventProcessorHostOpenStop(this.HostName);
 466            }
 0467        }
 468
 469        /// <summary>
 470        /// Stop processing events.  Does not return until the shutdown is complete.
 471        /// </summary>
 472        /// <returns></returns>
 473        public async Task UnregisterEventProcessorAsync() // throws InterruptedException, ExecutionException
 474        {
 0475            ProcessorEventSource.Log.EventProcessorHostCloseStart(this.HostName);
 476            try
 477            {
 0478                await this.PartitionManager.StopAsync().ConfigureAwait(false);
 0479            }
 0480            catch (Exception e)
 481            {
 482                // Log the failure but nothing really to do about it.
 0483                ProcessorEventSource.Log.EventProcessorHostCloseError(this.HostName, e.ToString());
 0484                throw;
 485            }
 486            finally
 487            {
 0488                ProcessorEventSource.Log.EventProcessorHostCloseStop(this.HostName);
 489            }
 0490        }
 491
 492        /// <summary>
 493        /// Convenience method for generating unique host names, safe to pass to the EventProcessorHost constructors
 494        /// that take a hostName argument.
 495        ///
 496        /// If a prefix is supplied, the constructed name begins with that string. If the prefix argument is null or
 497        /// an empty string, the constructed name begins with "host". Then a dash '-' and a unique ID are appended to
 498        /// create a unique name.
 499        /// </summary>
 500        /// <param name="prefix">String to use as the beginning of the name. If null or empty, a default is used.</param
 501        /// <returns>A unique host name to pass to EventProcessorHost constructors.</returns>
 502        static string CreateHostName(string prefix)
 503        {
 0504            if (string.IsNullOrEmpty(prefix))
 505            {
 0506                prefix = "host";
 507            }
 508
 0509            return prefix + "-" + Guid.NewGuid();
 510        }
 511
 512        internal EventHubClient CreateEventHubClient()
 513        {
 514            // Token provider already provided?
 0515            if (this.tokenProvider == null)
 516            {
 0517                return EventHubClient.CreateFromConnectionString(this.eventHubConnectionString);
 518            }
 519            else
 520            {
 0521                return EventHubClient.CreateWithTokenProvider(
 0522                    this.EndpointAddress,
 0523                    this.EventHubPath,
 0524                    this.tokenProvider,
 0525                    this.OperationTimeout,
 0526                    this.TransportType);
 527            }
 528        }
 529    }
 530}