| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 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 example of publishing events, extending the event data with custom metadata. |
| | 14 | | /// </summary> |
| | 15 | | /// |
| | 16 | | public class Sample08_PublishEventsWithCustomMetadata : IEventHubsSample |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The name of the sample. |
| | 20 | | /// </summary> |
| | 21 | | /// |
| 0 | 22 | | public string Name => nameof(Sample08_PublishEventsWithCustomMetadata); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// A short description of the sample. |
| | 26 | | /// </summary> |
| | 27 | | /// |
| 0 | 28 | | public string Description => "An example of publishing events, extending the event data with custom metadata."; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 32 | | /// </summary> |
| | 33 | | /// |
| | 34 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 35 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 36 | | /// |
| | 37 | | public async Task RunAsync(string connectionString, |
| | 38 | | string eventHubName) |
| | 39 | | { |
| | 40 | | // We will start by creating a producer client, using its default options. |
| | 41 | |
|
| 0 | 42 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 43 | | { |
| | 44 | | // Because an event consists mainly of an opaque set of bytes, it may be difficult for consumers of thos |
| | 45 | | // make informed decisions about how to process them. |
| | 46 | | // |
| | 47 | | // In order to allow event publishers to offer better context for consumers, event data may also contain |
| | 48 | | // in the form of a set of key/value pairs. This metadata is not used by, or in any way meaningful to, |
| | 49 | | // service; it exists only for coordination between event publishers and consumers. |
| | 50 | | // |
| | 51 | | // One common scenario for the inclusion of metadata is to provide a hint about the type of data contain |
| | 52 | | // so that consumers understand its format and can deserialize it appropriately. |
| | 53 | | // |
| | 54 | | // We will publish a small batch of events based on simple sentences, but will attach some custom metada |
| | 55 | | // pretend type names and other hints. Note that the set of metadata is unique to an event; there is no |
| | 56 | | // event in a batch to have the same metadata properties available nor the same data type for those prop |
| | 57 | |
|
| 0 | 58 | | var firstEvent = new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!")); |
| 0 | 59 | | firstEvent.Properties.Add("EventType", "com.microsoft.samples.hello-event"); |
| 0 | 60 | | firstEvent.Properties.Add("priority", 1); |
| 0 | 61 | | firstEvent.Properties.Add("score", 9.0); |
| | 62 | |
|
| 0 | 63 | | var secondEvent = new EventData(Encoding.UTF8.GetBytes("Goodbye, Event Hubs!")); |
| 0 | 64 | | secondEvent.Properties.Add("EventType", "com.microsoft.samples.goodbye-event"); |
| 0 | 65 | | secondEvent.Properties.Add("priority", "17"); |
| 0 | 66 | | secondEvent.Properties.Add("blob", true); |
| | 67 | |
|
| 0 | 68 | | using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); |
| 0 | 69 | | eventBatch.TryAdd(firstEvent); |
| 0 | 70 | | eventBatch.TryAdd(secondEvent); |
| | 71 | |
|
| 0 | 72 | | await producerClient.SendAsync(eventBatch); |
| | 73 | |
|
| 0 | 74 | | Console.WriteLine("The event batch has been published."); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We |
| | 78 | | // have no further obligations. |
| | 79 | |
|
| 0 | 80 | | Console.WriteLine(); |
| 0 | 81 | | } |
| | 82 | | } |
| | 83 | | } |