One of our test servers has proxy settings and I was able to detect what proxy to use by using a 1 line powershell command.
(new-object System.Net.WebClient).Proxy.GetProxy("https://www.google.com")
However, if I instantiate a WebClient object in .Net Core the proxy is null. Our current solution is to simply set the proxy for all outgoing connections as part of the environment configuration. This worked fine for some of our simpler services that only contacted one endpoint that needed to be proxied, however I am now working on a fix where one endpoint needs to be proxied and another not.
My options are
- Add an exclusion list for endpoints not to be proxied
- Get the proxy settings from the operating system somehow
This question is asking for help for number 2. I can implement option 1 with no issue.
My best guess is that the proxy information is not available due to changes in .Net architecture. I found the following illuminating quote. (https://github.com/dotnet/runtime/issues/24756) We are using dot net core 3.
As of .NET Core 2.1, the HTTP protocol stack used in .NET Core was changed. It no longer uses native Windows WinHTTP nor Linux 'libcurl'. Those are considered now the 'legacy' stacks. We have a new HTTP protocol stack called "SocketsHttpHandler". It used by default.
So basically I think I am looking for some magic code to get proxy information from the old Windows network stack if running on Windows.
bool isWindows = System.Runtime.InteropServices.RuntimeInformation
.IsOSPlatform(OSPlatform.Windows);
if (isWindows) {
// Do magic get proxy settings secret sauce.
}