Search code examples
azureasp.net-corehttpclient

Azure .NET Core WebAPI complains about HttpClient.GetAsync()


Am running .netcore 2.2 WebAPI and the application runs well, returns a list of all printer vendors. The printerVendors is a method in a PrintersController running as a windows service self hosting .NET application. When i publish the API to my azure resource WebAPI .NET Core, i get an error in swagger that:

"An attempt was made to access a socket in a way forbidden by its access permissions".

Furthermore, the code points to the client.GetAsync or client.PostAsync lines.

I have tried to keep one instance of HttpClient but it has solved the problem. Googling around, i see something like Azure is restricting the number of HttpC Calls and that i need to extend that limit. I dont know if this is the solution as am new to this kind of stuff

private static HttpClient client = new HttpClient { BaseAddress = new Uri("http://localhost:43634/") };

[HttpGet("GetPrinterVendorslist")]
public async Task<IActionResult> GetPrinterVendorslist()
{
    _logger?.LogDebug("'{0}' has been invoked", nameof(GetPrinterVendorslist));

    try
    {
        client.DefaultRequestHeaders.Accept.Clear();
        var response = await client.GetAsync("api/printers/GetVendorsList");
        var httpContent = response.Content;

        var result = await httpContent.ReadAsStringAsync();
        var variables = JsonConvert.DeserializeObject(result);

        return Ok(variables);
    }
    catch (Exception ex)
    {
        _logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(GetPrinterVendorslist), ex);
        return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
     }
}

Solution

  • Solved the problem by adding an exception in the Network configurations. I also listed the port that runs my local windows service too.