0

Getting following issue:

[DEBUG] .NET DNS resolution failed: nodename nor servname provided, or not known
[DEBUG] Inner Exception Type: Azure.RequestFailedException

when trying to connect to my Azure AI Foundry Agent (Model + endpoints call works fine). I am instantiating an AgentsClient object and have set my var connectionString = System.Environment.GetEnvironmentVariable("PROJECT_CONNECTION_STRING"); correctly by getting it from the overview for my project in the Azure AI Foundry portal, under Project details > Project connection string.

Code I ran:

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable disable
using Azure.Identity;
using Azure.Core;
namespace Azure.AI.Projects;
public class Sample_Agent
{
 static async Task Main()
 {
 Console.WriteLine($"Application started at: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
 Console.WriteLine("Initializing agent configuration...");
 
 // Use explicit connection string as requested
 var connectionString = "<project connection string I scrubbed, format: <HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>>";
 Console.WriteLine($"[DEBUG] Using connection string: {connectionString.Substring(0, Math.Min(connectionString.Length, 20))}... (masked)");
 Console.WriteLine($"[DEBUG] Machine Name: {Environment.MachineName}");
 Console.WriteLine($"[DEBUG] OS Version: {Environment.OSVersion}");
 Console.WriteLine($"[DEBUG] Current Directory: {Environment.CurrentDirectory}");
 try
 {
 var host = "eastus.api.azureml.ms";
 Console.WriteLine($"[DEBUG] .NET DNS test for {host}");
 var entry = System.Net.Dns.GetHostEntry(host);
 Console.WriteLine($"[DEBUG] .NET DNS resolved: {entry}");
 }
 catch (Exception ex)
 {
 Console.WriteLine($"[DEBUG] .NET DNS resolution failed: {ex.Message}");
 }
 var options = new AIProjectClientOptions();
 options.Retry.MaxRetries = 1;
 AgentsClient client = new AgentsClient(connectionString, new DefaultAzureCredential(), options);
 
 try 
 {
 Console.WriteLine("[DEBUG] Attempting to retrieve agent by ID");
 Response<Agent> agentResponse = await client.GetAgentAsync("<agent id I scrubbed>");
 Agent agent = agentResponse.Value;
 Console.WriteLine($"✓ Agent retrieved successfully (ID: {agent.Id})");
 // Verify agent in list
 Console.WriteLine("\nVerifying agent in list...");
 Response<PageableList<Agent>> agentListResponse = await client.GetAgentsAsync();
 Console.WriteLine($"✓ Found {agentListResponse.Value.Data.Count} agent(s) in total");
 // Step 2: Create a thread
 Console.WriteLine("\n=== Creating Thread ===");
 Response<AgentThread> threadResponse = await client.CreateThreadAsync();
 AgentThread thread = threadResponse.Value;
 Console.WriteLine($"✓ Thread created successfully (ID: {thread.Id})");
 // Step 3: Add a message
 Console.WriteLine("\n=== Adding Message to Thread ===");
 string userMessage = "I need to solve the equation `3x + 11 = 14`. Can you help me?";
 Console.WriteLine($"User message: {userMessage}");
 Response<ThreadMessage> messageResponse = await client.CreateMessageAsync(
 thread.Id,
 MessageRole.User,
 userMessage);
 ThreadMessage message = messageResponse.Value;
 Console.WriteLine($"✓ Message added successfully (ID: {message.Id})");
 // Verify message in thread
 Console.WriteLine("\nVerifying message in thread...");
 Response<PageableList<ThreadMessage>> messagesListResponse = await client.GetMessagesAsync(thread.Id);
 Console.WriteLine($"✓ Found {messagesListResponse.Value.Data.Count} message(s) in thread");
 // Step 4: Run the agent
 Console.WriteLine("\n=== Starting Agent Run ===");
 Response<ThreadRun> runResponse = await client.CreateRunAsync(
 thread.Id,
 agent.Id,
 additionalInstructions: "");
 ThreadRun run = runResponse.Value;
 Console.WriteLine($"✓ Run initiated (ID: {run.Id})");
 // Monitor run progress
 int pollCount = 0;
 Console.WriteLine("\nMonitoring run progress:");
 do
 {
 pollCount++;
 Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Status: {runResponse.Value.Status} (Poll #{pollCount})");
 await Task.Delay(TimeSpan.FromMilliseconds(500));
 runResponse = await client.GetRunAsync(thread.Id, runResponse.Value.Id);
 }
 while (runResponse.Value.Status == RunStatus.Queued
 || runResponse.Value.Status == RunStatus.InProgress);
 Console.WriteLine($"\n✓ Run completed with status: {runResponse.Value.Status}");
 if (runResponse.Value.LastError != null)
 {
 Console.WriteLine($"⚠ Last error: {runResponse.Value.LastError.Message}");
 }
 // Get final messages
 Console.WriteLine("\n=== Final Message History ===");
 Response<PageableList<ThreadMessage>> afterRunMessagesResponse = await client.GetMessagesAsync(thread.Id);
 IReadOnlyList<ThreadMessage> messages = afterRunMessagesResponse.Value.Data;
 Console.WriteLine($"Retrieved {messages.Count} total messages\n");
 Console.WriteLine("Message Timeline:");
 Console.WriteLine("----------------");
 foreach (ThreadMessage threadMessage in messages)
 {
 Console.WriteLine($"\n[{threadMessage.CreatedAt:yyyy-MM-dd HH:mm:ss}]");
 Console.WriteLine($"Role: {threadMessage.Role}");
 Console.WriteLine("Content:");
 foreach (MessageContent contentItem in threadMessage.ContentItems)
 {
 if (contentItem is MessageTextContent textItem)
 {
 Console.WriteLine($" {textItem.Text}");
 }
 else if (contentItem is MessageImageFileContent imageFileItem)
 {
 Console.WriteLine($" <image from ID: {imageFileItem.FileId}>");
 }
 }
 Console.WriteLine("----------------");
 }
 }
 catch (Exception ex)
 {
 Console.WriteLine($"\n❌ Error occurred: {ex.Message}");
 Console.WriteLine($"[DEBUG] Exception Type: {ex.GetType().FullName}");
 Console.WriteLine($"[DEBUG] Stack trace: {ex.StackTrace}");
 if (ex.InnerException != null)
 {
 Console.WriteLine($"[DEBUG] Inner Exception: {ex.InnerException.Message}");
 Console.WriteLine($"[DEBUG] Inner Exception Type: {ex.InnerException.GetType().FullName}");
 Console.WriteLine($"[DEBUG] Inner Exception Stack trace: {ex.InnerException.StackTrace}");
 }
 if (ex is Azure.RequestFailedException reqEx)
 {
 Console.WriteLine($"[DEBUG] Azure RequestFailedException Status: {reqEx.Status}");
 Console.WriteLine($"[DEBUG] Azure RequestFailedException ErrorCode: {reqEx.ErrorCode}");
 }
 }
 
 Console.WriteLine($"\nApplication completed at: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
 }
}

Guide followed: https://learn.microsoft.com/en-us/azure/ai-services/agents/quickstart?pivots=programming-language-csharp

Tried running more DNS tests & trying to System.Net.Dns.GetHostEntry() on my project connectionString is the one that fails:

[DEBUG] .NET DNS test for eastus.api.azureml.ms
//connectionString System.Net.Dns.GetHostEntry() test:
[DEBUG] .NET DNS resolution failed: nodename nor servname provided, or not known
[DEBUG] .NET DNS test for vienna-eastus-ip-ingress-nginx-02.eastus.cloudapp.azure.com
[DEBUG] .NET DNS resolved: 48.211.42.162
[DEBUG] .NET DNS test for www.google.com
[DEBUG] .NET DNS resolved: 142.250.72.100
P Sampath
4,2752 gold badges7 silver badges27 bronze badges
asked May 16 at 2:01
1
  • Try running nslookup eastus.api.azureml.ms to verify if DNS resolution works. if it fails, check your proxy or network firewall settings Commented May 22 at 6:36

1 Answer 1

0

.NET DNS resolution failed: nodename nor servname provided, or not known

The error you're encountering occurs when your system can't find the IP address for the hostname in the connection string (eastus.api.azureml.ms). Other websites like www.google.com work fine, so it's likely a DNS or network block specific to this Azure AI endpoint.

To resolve the issue,

  • Check if your machine can resolve the Azure AI Foundry host,
nslookup eastus.api.azureml.ms

If it fails, DNS to Azure is blocked or misconfigured. if it works, the issue is likely within your app or environment.

Please refer this MSdoc to know about Azure DNS troubleshoot.

  • You might be on a VPN, or behind a proxy that blocks outbound DNS or traffic to Azure services.

If behind a proxy, set these environment variables before running the app,

set HTTPS_PROXY=http://your-proxy:port
set HTTP_PROXY=http://your-proxy:port

you can also set in your code ,

var httpClientHandler = new HttpClientHandler
{
 Proxy = new WebProxy("http://proxy-address:port"),
 UseProxy = true
};
  • Verify the Connection String Format, sometimes DNS resolution fails if the connection string is malformed. connection string format:
<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<ProjectName>

Then, extract and test just the hostname,

var host = connectionString.Split(';')[0];
System.Net.Dns.GetHostEntry(host);

If this fails, your connection string is likely malformed or the hostname is incorrect.

These steps will help in identifying whether the issue is due to DNS, proxy, or environmental misconfiguration.

answered May 22 at 10:08
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.