| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Net; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Messaging.EventHubs.Producer; |
| | 8 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs.Samples |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// An introduction to Event Hubs, exploring additional options for creating the |
| | 14 | | /// different Event Hub clients. |
| | 15 | | /// </summary> |
| | 16 | | /// |
| | 17 | | public class Sample02_ClientWithCustomOptions : IEventHubsSample |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The name of the sample. |
| | 21 | | /// </summary> |
| | 22 | | /// |
| 0 | 23 | | public string Name => nameof(Sample02_ClientWithCustomOptions); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// A short description of the sample. |
| | 27 | | /// </summary> |
| | 28 | | /// |
| 0 | 29 | | public string Description => "An introduction to Event Hubs, exploring additional options for creating the diffe |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 33 | | /// </summary> |
| | 34 | | /// |
| | 35 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 36 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 37 | | /// |
| | 38 | | public async Task RunAsync(string connectionString, |
| | 39 | | string eventHubName) |
| | 40 | | { |
| | 41 | | // The clients for an Event Hub client each offer additional options on creation, allowing you to control di |
| | 42 | | // should your scenario have needs that differ from the common defaults. If you choose not to provide these |
| | 43 | | // suitable to most scenarios are used. |
| | 44 | | // |
| | 45 | | // Each different Event Hub client allows you to customize how it interacts with the Event Hubs service, suc |
| | 46 | | // to the service by specifying the transport that communication should use and whether a proxy should be us |
| | 47 | | // note that a proxy is only supported when using WebSockets as a transport; it isn't compatible with raw TC |
| | 48 | | // |
| | 49 | | // The Event Hub clients each offer a common set of options, such as specifying the timeout and retry approa |
| | 50 | | // Event Hubs service. A specific client will potentially allow you to customize behavior specific to its r |
| | 51 | | // |
| | 52 | |
|
| | 53 | | // This sample will customize the transport for the connection, using WebSockets and will adjust some of the |
| | 54 | | // illustration. |
| | 55 | |
|
| 0 | 56 | | var producerOptions = new EventHubProducerClientOptions |
| 0 | 57 | | { |
| 0 | 58 | | ConnectionOptions = new EventHubConnectionOptions |
| 0 | 59 | | { |
| 0 | 60 | | TransportType = EventHubsTransportType.AmqpWebSockets, |
| 0 | 61 | | Proxy = (IWebProxy)null |
| 0 | 62 | | }, |
| 0 | 63 | |
|
| 0 | 64 | | RetryOptions = new EventHubsRetryOptions |
| 0 | 65 | | { |
| 0 | 66 | | MaximumRetries = 5, |
| 0 | 67 | | TryTimeout = TimeSpan.FromMinutes(1) |
| 0 | 68 | | } |
| 0 | 69 | | }; |
| | 70 | |
|
| 0 | 71 | | await using (var producer = new EventHubProducerClient(connectionString, eventHubName, producerOptions)) |
| | 72 | | { |
| | 73 | | // Using the client, we will inspect the Event Hub that it is connected to, getting |
| | 74 | | // access to metadata about it. |
| | 75 | |
|
| 0 | 76 | | EventHubProperties properties = await producer.GetEventHubPropertiesAsync(); |
| | 77 | |
|
| 0 | 78 | | Console.WriteLine("The Event Hub has the following properties:"); |
| 0 | 79 | | Console.WriteLine($"\tThe path to the Event Hub from the namespace is: { properties.Name }"); |
| 0 | 80 | | Console.WriteLine($"\tThe Event Hub was created at: { properties.CreatedOn.ToString("yyyy-MM-dd hh:mm:ss |
| 0 | 81 | | Console.WriteLine("\tThe Event Hub has the following partitions:"); |
| | 82 | |
|
| 0 | 83 | | foreach (string partitionId in properties.PartitionIds) |
| | 84 | | { |
| 0 | 85 | | Console.WriteLine($"\t\tPartition Id: { partitionId }"); |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We have no |
| | 90 | | // further obligations. |
| | 91 | |
|
| 0 | 92 | | Console.WriteLine(); |
| 0 | 93 | | } |
| | 94 | | } |
| | 95 | | } |