.Net Core 8 Web Api hosted in Azure App Service invoking third party/external API

Amit Kholi_Optimus 0 Reputation points
2024-10-03T18:31:09.92+00:00

Hello,

Thanks for reading this.

Environment: .Net Core 8 Web Api hosted in Azure App Service invoking third party/external API

In local environment running in Visual Studio 2022 this is not an issue.

When an external/third party endpoint is invoked - a port number is incorrectly appended to the URL - this results in an error, the exception message is: "No such host is known. (testnomsapi.xyzxyz.com:443)"

The code creates an instance of HttpClient in conjunction with HttpRequestMessage - this is in fact boiler plate code provided by the vendor whose service endpoint is being invoked.

There are no IP restrictions by the vendor for their test environment.

The Azure environment is provided the client - we are a service company providing an integration adapter API.

While the request object created in both environments - Local & DEV(Azure) appears to be the same, the one in Azure creates an endpoint that has the Port suffixed resulting in the error pasted above.

Grateful for any insight.

Thanks and regards,

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,925 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shree Hima Bindu Maganti 475 Reputation points Microsoft Vendor
    2024-10-22T15:51:47.2366667+00:00

    Hi @Amit Kholi_Optimus ,
    Welcome to the Microsoft Q&A Platform!
    It appears that the root cause of your issue lies in the automatic port suffix (:443) being added to the URL when the .NET Core 8 Web API is hosted in Azure App Service, but not when running locally in Visual Studio 2022.But in this case, it seems to be causing an issue by appending unnecessarily to the endpoint.
    Explicitly Set URL in HttpRequestMessage:

    • Ensure the HttpRequestMessage explicitly sets the URL without any port
        var request = new HttpRequestMessage(HttpMethod.Get, new Uri("https://testnomsapi.xyzxyz.com"));
        request.Headers.Host = "testnomsapi.xyzxyz.com";
      
    • Disable Proxy in HttpClientHandler: Disable any default proxy settings in Azure by configuring HttpClientHandler:
      var handler = new HttpClientHandler { UseProxy = false };
    • Check BaseAddress in HttpClient: Ensure no unwanted port is being appended in the BaseAddress setting:
      client.BaseAddress = new Uri("https://testnomsapi.xyzxyz.com");
    • Examine App Settings in Azure: Check for any environment variables that may impact the request, such as WEBSITE_ALT_PORT.
    • Use Application Insights for Logging: Add detailed logging to track where the port is being added.
      By doing this you should be able to prevent the port from being appended to your third-party API call.
      If the answer is helpful, please click "Accept Answer" and kindly upvote it.
    0 comments No comments

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.