I tried to insert the data into event hub for that followed this link to created the Free Trial event hub in azure portal and configured the connectionstring, eventHubName in my sample .Net application(referred link)
sample code:
class Program
{
const string eventHubName = "<eventHubName>";
const string connectionString = @"<connectionString>";
public static async Task Main(string[] args)
{
await EventHubIngestionAsync();
}
public static async Task EventHubIngestionAsync()
{
await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName))
{
int counter = 0;
for (int i = 0; i < 100; i )
{
int recordsPerMessage = 3;
try
{
var records = Enumerable
.Range(0, recordsPerMessage)
.Select(recordNumber => $"{{\"timeStamp\": \"{DateTime.UtcNow.AddSeconds(100 * counter)}\", \"name\": \"{$"name {counter}"}\", \"metric\": {counter recordNumber}, \"source\": \"EventHubMessage\"}}");
string recordString = string.Join(Environment.NewLine, records);
EventData eventData = new EventData(Encoding.UTF8.GetBytes(recordString));
Console.WriteLine($"sending message {counter}");
// Optional "dynamic routing" properties for the database, table, and mapping you created.
//eventData.Properties.Add("Table", "TestTable");
//eventData.Properties.Add("IngestionMappingReference", "TestMapping");
//eventData.Properties.Add("Format", "json");
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
eventBatch.TryAdd(eventData);
IEnumerable<EventData> dd = null;
await producerClient.SendAsync(dd);
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
Console.ResetColor();
}
counter = recordsPerMessage;
}
}
}
}
When running CreateBatchAsync getting the 'A connection attempt failed because the connected party did not properly respond'.
I tried both connection string and Access control method getting the same error.
CodePudding user response:
That exception indicates that the client was unable to establish a connection with the Event Hubs service. The most common reason for this specific failure is that the network does not allow traffic over the AMQP ports (5761, 5762).
I'd suggest configuring the client to use AMQP over web sockets, as shown in this sample. That will direct traffic over port 443, which is more commonly allowed in restricted networks.