-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
39 lines (32 loc) · 1.69 KB
/
Program.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
//-----------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
// See https://aka.ms/new-console-template for more information
using System.Text;
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using Bogus;
using Newtonsoft.Json;
var items = new Faker<Item>()
.RuleFor(item => item.Id, fake => Guid.NewGuid().ToString())
.RuleFor(item => item.FirstName, fake => fake.Person.FirstName)
.RuleFor(item => item.LastName, fake => fake.Person.LastName)
.Generate(count: 10);
var connectionString = await HarnessUtility.GetEventHubConnectionStringAsync(connectionName: System.Environment.GetEnvironmentVariable("HARNESS_EVENTHUB_CONNECTIONSTRING_NAME"));
var producerClient = new EventHubProducerClient(connectionString: connectionString, eventHubName: System.Environment.GetEnvironmentVariable("HARNESS_EVENTHUB_NAME"));
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync().ConfigureAwait(continueOnCapturedContext: false); ;
foreach(var item in items)
{
var itemAsString = JsonConvert.SerializeObject(value: item);
if (!eventBatch.TryAdd(eventData: new EventData(eventBody: Encoding.UTF8.GetBytes(s: itemAsString))))
{
throw new Exception(message: $"Event {itemAsString} is too large for the batch and cannot be sent.");
}
else
{
Console.WriteLine(value: itemAsString);
}
}
await producerClient.SendAsync(eventBatch: eventBatch).ConfigureAwait(continueOnCapturedContext: false);
await producerClient.DisposeAsync().ConfigureAwait(continueOnCapturedContext: false);
Console.WriteLine("Done!");