DidneyTharWarths
DidneyTharWarths

Reputation: 27

Azure Functions HttpClient No such host is known on Request

I'm trying to see if an API is up but when I publish the code to Azure, the HttpClient fails to get anything because when it tries to .SendAsync(); the error message I get from Azure is "No Such Host is Known" but the code does work unaltered on my local environment.

Any idea from this what might be missing or would be the cause for the HttpClient Get to be failing?

public class ConnectionTester
    {
        [FunctionName("ConnectionTester")]
        public static async Task Run([TimerTrigger("* */30 * * * *")] TimerInfo connectionTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", strAuth);
            HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, strLink);
            var response = await client.SendAsync(newRequest);
            
            if (response.IsSuccessStatusCode)
            {
                log.LogInformation("Success");
            }
            else
            {
                log.LogError("Error");
            }
        }
    }

Upvotes: 2

Views: 10782

Answers (1)

anon
anon

Reputation:

I have followed the same code given in the question and reproduced below:

Stack: Azure Functions (.NET 6) - Timer Trigger

Function1.cs

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace KrishFunctionApp1205
{
    public class Function1
    {
        [FunctionName("Function1")]
        public async Task RunAsync([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            var strLink = "https://stackoverflow.com/";
            var strAuth = "Key05";
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", strAuth);
            HttpRequestMessage newRequest = new HttpRequestMessage(HttpMethod.Get, strLink);
            var response = await client.SendAsync(newRequest);

            if (response.IsSuccessStatusCode)
                log.LogInformation("Okidoki");
            else
                log.LogError($"{response.StatusCode} {response.ReasonPhrase}: ");
        }
    }
}

local.settings.json:

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

host.json:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Result when running locally:

Result

Result when running in Azure:

enter image description here


Note: Few steps to resolve below error is:

HttpClient No such host is known on Request

  • It might be DNS issue, check the URI is working or not.

  • The requests you're trying to send to the URI being blocked by the corporate firewall or your system firewall.

  • Change the Port number in the Azure Functions Project and Run the function. (Refer here to know how to change the port number in Azure Functions)

  • Upgrade/Update the .NET Project SDK if any updates available in Visual Studio.

  • Check the Connection String is pointed to correct value. For running locally and to use local storage emulator, then:

    "AzureWebJobsStorage": "UseDevelopmentStorage=true"

    To use Azure Storage account and run the function locally, then

    "AzureWebJobsStorage": "<Azure-Storage-Account-Connection-String>"

This connecting string you can get from the Azure Portal > Storage Account > Access Keys > Connection String.


Upvotes: 0

Related Questions