I'm facing an issue with outbound TCP port exhaustion on my Azure App Service running with a P1v2 SKU. I'm looking for a way to monitor the outbound TCP usage because it peaks only briefly, typically for around 30 minutes. My goal is to implement active monitoring and introduce delays to manage rapid port consumption. I'm trying to use the IPGlobalProperties.GetIPGlobalProperties method followed by GetTcpIPv4Statistics, but it seems this API isn't allowed in the App Service environment. Does anyone know why this is the case? Additionally, is there an alternative API that I can use to track the outbound TCP ports specifically for the process that's running? I've already checked the "Diagnose and solve problems tool" in the Azure Portal, but I want to be proactive instead of reactive in handling port usage.
3 Answers
To actively monitor the situation, you should set up an Azure Monitor alert based on the relevant metrics. The alert can trigger a webhook that connects to your app service and applies any necessary fixes.
It sounds like the issue might be related to how you're using HttpClient. Make sure to register the HttpClientFactory in your Startup or Program file with AddHttpClient. Then, for your outbound calls, consider injecting IHttpClientFactory to create your clients rather than using `new HttpClient`. This could help mitigate socket exhaustion problems.
I appreciate the suggestion! I'm cautious with HTTP usage, but I believe my issue is more with outbound AMQPS connections without a message processor. I see that I didn't clarify this in my post—I'm actually running a webjob for continuous message consumption. Is there a way to monitor sockets at a lower level in App Service?
I'm curious about how you're running out of outbound sockets. It seems like there could be an HTTP mishap happening. If that's not the issue, consider adding a NAT Gateway to your setup; it could help manage your outbound connections better.

That sounds like a complex setup just to monitor my port usage! I've never worked in an environment where I could check my own network ports directly.