-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (118 loc) · 6.41 KB
/
Copy pathProgram.cs
File metadata and controls
140 lines (118 loc) · 6.41 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Azure.Messaging.EventHubs;
using Azure.Messaging.EventHubs.Producer;
using Microsoft.Extensions.Configuration;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Load configuration from appsettings.json
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true)
.Build();
string connectionString = configuration["EventHub:ConnectionString"] ??
throw new InvalidOperationException("EventHub:ConnectionString not found in configuration");
string eventHubName = configuration["EventHub:EventHubName"] ??
throw new InvalidOperationException("EventHub:EventHubName not found in configuration");
bool useAppGateway = bool.Parse(configuration["EventHub:UseAppGateway"] ?? "false");
string? appGatewayEndpoint = configuration["EventHub:AppGatewayEndpoint"];
int appGatewayPort = int.Parse(configuration["EventHub:AppGatewayPort"] ?? "5671");
bool useWebSockets = bool.Parse(configuration["EventHub:UseWebSockets"] ?? "false");
Console.WriteLine("Starting Event Hub sender...");
Console.WriteLine($"Event Hub Name: {eventHubName}");
Console.WriteLine($"Using App Gateway: {useAppGateway}");
Console.WriteLine($"Transport: {(useWebSockets ? "WebSockets (Port 443)" : "AMQP (Port 5671)")}");
try
{
if (useAppGateway && !string.IsNullOrEmpty(appGatewayEndpoint))
{
Console.WriteLine($"App Gateway Endpoint: {appGatewayEndpoint}:{appGatewayPort}");
await SendEventsViaAppGateway(connectionString, eventHubName, appGatewayEndpoint, appGatewayPort, useWebSockets);
}
else
{
await SendEventsDirectly(connectionString, eventHubName);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred: {ex.Message}");
Console.WriteLine($"Stack trace: {ex.StackTrace}");
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static async Task SendEventsDirectly(string connectionString, string eventHubName)
{
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
// Create a batch of events
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
// Add events to the batch
var event1 = new EventData(Encoding.UTF8.GetBytes($"Event 1 - Timestamp: {DateTime.UtcNow}"));
var event2 = new EventData(Encoding.UTF8.GetBytes($"Event 2 - Random value: {new Random().Next(1, 1000)}"));
var event3 = new EventData(Encoding.UTF8.GetBytes($"Event 3 - Message: Hello from C# Event Hub sender!"));
if (!eventBatch.TryAdd(event1))
{
throw new Exception("Event 1 is too large for the batch and cannot be sent.");
}
if (!eventBatch.TryAdd(event2))
{
throw new Exception("Event 2 is too large for the batch and cannot be sent.");
}
if (!eventBatch.TryAdd(event3))
{
throw new Exception("Event 3 is too large for the batch and cannot be sent.");
}
// Send the batch of events to the event hub
await producerClient.SendAsync(eventBatch);
Console.WriteLine($"Successfully sent {eventBatch.Count} events to Event Hub: {eventHubName} (Direct connection)");
}
}
static async Task SendEventsViaAppGateway(string connectionString, string eventHubName, string appGatewayEndpoint, int appGatewayPort, bool useWebSockets)
{
// Create EventHubClientOptions with custom endpoint address and transport type
var clientOptions = new EventHubProducerClientOptions
{
ConnectionOptions = new EventHubConnectionOptions
{
CustomEndpointAddress = new Uri($"sb://{appGatewayEndpoint}:{appGatewayPort}"),
TransportType = useWebSockets ? EventHubsTransportType.AmqpWebSockets : EventHubsTransportType.AmqpTcp
}
};
string transportInfo = useWebSockets ? "WebSockets over HTTPS" : "AMQP over TCP";
Console.WriteLine($"Using custom endpoint: sb://{appGatewayEndpoint}:{appGatewayPort}");
Console.WriteLine($"Transport: {transportInfo}");
Console.WriteLine($"Original connection string endpoint preserved for authentication");
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName, clientOptions))
{
// Create a batch of events
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
// Add events to the batch
string transportType = useWebSockets ? "WebSockets" : "AMQP";
var event1 = new EventData(Encoding.UTF8.GetBytes($"Event 1 via App Gateway ({transportType}) - Timestamp: {DateTime.UtcNow}"));
var event2 = new EventData(Encoding.UTF8.GetBytes($"Event 2 via App Gateway ({transportType}) - Random value: {new Random().Next(1, 1000)}"));
var event3 = new EventData(Encoding.UTF8.GetBytes($"Event 3 via App Gateway ({transportType}) - Message: Hello from C# Event Hub sender via App Gateway!"));
if (!eventBatch.TryAdd(event1))
{
throw new Exception("Event 1 is too large for the batch and cannot be sent.");
}
if (!eventBatch.TryAdd(event2))
{
throw new Exception("Event 2 is too large for the batch and cannot be sent.");
}
if (!eventBatch.TryAdd(event3))
{
throw new Exception("Event 3 is too large for the batch and cannot be sent.");
}
// Send the batch of events to the event hub through App Gateway
await producerClient.SendAsync(eventBatch);
string portInfo = useWebSockets ? "443 (WebSockets)" : $"{appGatewayPort} (AMQP)";
Console.WriteLine($"Successfully sent {eventBatch.Count} events to Event Hub: {eventHubName} (via App Gateway on port {portInfo})");
}
}
}