forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample03_PublishAnEvent.cs
executable file
·78 lines (65 loc) · 3.7 KB
/
Sample03_PublishAnEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs.Samples.Infrastructure;
namespace Azure.Messaging.EventHubs.Samples
{
/// <summary>
/// An introduction to publishing events, using a simple <see cref="EventHubProducerClient" />.
/// </summary>
///
public class Sample03_PublishAnEvent : IEventHubsSample
{
/// <summary>
/// The name of the sample.
/// </summary>
///
public string Name { get; } = nameof(Sample03_PublishAnEvent);
/// <summary>
/// A short description of the sample.
/// </summary>
///
public string Description { get; } = "An introduction to publishing events, using a simple Event Hub producer client.";
/// <summary>
/// Runs the sample using the specified Event Hubs connection information.
/// </summary>
///
/// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should target.</param>
/// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should run against.</param>
///
public async Task RunAsync(string connectionString,
string eventHubName)
{
// To publish events, we will need to create a producer client. Like any client, our Event Hub producer manages resources
// and should be explicitly closed or disposed, but it is not necessary to do both. In this example, we will take
// advantage of the new asynchronous dispose to ensure that we clean up our producer client when we are
// done or when an exception is encountered.
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
// An Event Hub producer is not associated with any specific partition. When publishing events,
// it will allow the Event Hubs service to route the event to an available partition.
//
// Allowing automatic routing of partitions is recommended when:
// - The publishing of events needs to be highly available.
// - The event data should be evenly distributed among all available partitions.
// An event is represented by an arbitrary collection of bytes and metadata. Event Hubs does not make any
// assumptions about the data nor attempt to perform any operations on it; you are free to create the data
// in whatever form makes sense for your scenario.
//
// In our case, we will translate a simple sentence into bytes and send it to our Event Hub.
var eventData = new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!"));
// When the producer sends the event, it will receive an acknowledgment from the Event Hubs service; so
// long as there is no exception thrown by this call, the service is now responsible for delivery. Your
// event data will be published to one of the Event Hub partitions, though there may be a (very) slight
// delay until it is available to be consumed.
await producerClient.SendAsync(eventData);
Console.WriteLine("The event has been published.");
}
// At this point, our client has passed its "using" scope and has safely been disposed of. We
// have no further obligations.
Console.WriteLine();
}
}
}