Azure Function App with Hybrid Connection throwing `An attempt was made to access a socket in a way forbidden by its access permissions`

Anonymous
2025-02-27T18:50:52.7866667+00:00

Hello, I'm trying to call rest endpoint of my private network server using Azure Function App. My server has only one dummy endpoint (localhost:5555/hello) that just returns "Hello world" message.

I've created c# based Function App in Azure with on HTTP-triggered Function.

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    // Sending a GET request to the specified URL
    using (HttpClient client = new HttpClient())
    {
        try
        {
            HttpResponseMessage response = await client.GetAsync("http://myhostname:5555/hello");

            // Check if the response is successful
            if (response.IsSuccessStatusCode)
            {
                string responseData = await response.Content.ReadAsStringAsync();
                return new OkObjectResult($"Hello, {name}. This HTTP triggered function executed successfully. External response: {responseData}");
            }
            else
            {
                log.LogError($"Error: Received status code {response.StatusCode}");
                return new ObjectResult("Error occurred while contacting external service.") { StatusCode = (int)response.StatusCode };
            }
        }
        catch (HttpRequestException ex)
        {
            log.LogError($"Request exception: {ex.Message}");
            return new ObjectResult("External service is not reachable.") { StatusCode = StatusCodes.Status503ServiceUnavailable };
        }
        catch (Exception ex)
        {
            log.LogError($"General exception: {ex.Message}");
            return new ObjectResult("An unexpected error occurred.") { StatusCode = StatusCodes.Status500InternalServerError };
        }
    }
}

From my server I can simply call http:/myhostname:5555/hello endpoint.

I've also connected (using Hybrid Connection Manager) my server to Azure. I have Azure status connected for myhostname:5555.

But when I run my Function app in Azure it keeps throwing [Error] Request exception: An attempt was made to access a socket in a way forbidden by its access permissions. (myhostname:5555)

p.s.

There's nothing running on myhostname:5555 (based on netstat -ant)

I got myhostname with [System.Net.Dns]::GetHostByName($env:computerName)

I will appreciate any help with the issue! Maybe azure functions isn't suitable for my purposes.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.