Tamil Rk

Tamil Rk

  • NA
  • 442
  • 15.8k

Event hub CreateBatchAsync - A connection attempt failed

Feb 3 2023 6:16 AM

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 the connection string and Access control method getting the same error.


Answers (1)