1
votes

I have a C# .NET program (4.7.1) which I want to use the default system proxy if one is available.

When I put the following code in my App.Config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true">
    </defaultProxy>
  </system.net>
 ... rest of the file
</configuration>

The application crashes on startup in KERNELBASE.dll with no error and exits immediately.

I have setup a proxy on localhost using fiddler (to do some testing)

I can find the following error in the event logs which is not very useful:

Faulting application name: myprogram.exe, version: 0.01.6652.23883, time stamp: 0x5aaf246f
Faulting module name: KERNELBASE.dll, version: 10.0.16299.15, time stamp: 0x2cd1ce3d
Exception code: 0xe0434352
Fault offset: 0x001008b2
Faulting process id: 0x1220
Faulting application start time: 0x01d3bf2e95ca9d05
Faulting application path: C:\source\myprogram.exe
Faulting module path: C:\Windows\System32\KERNELBASE.dll
Report Id: 5a60273b-637f-4dac-ae09-5539fb563884
Faulting package full name: 
Faulting package-relative application ID: 

Any ideas where I am going wrong and how to get default proxy working in a C# .NET program?

2
is it not compulsory to specify proxy address ?rahulaga_dev
The goal was to automatically detect the proxy. I have no idea what proxy my customers may or may not be using.rolls
@rolls, I know it's been a while since you posted the issue, but did you resolve it?jambis
yes I did. I'll add answer for what I used.rolls

2 Answers

2
votes

According to the docs:

The proxy element defines a proxy server for an application. If this element is missing from the configuration file, then the .NET Framework will use the proxy settings in Internet Explorer.

I would venture to guess that the machine you are attempting to run this on doesn't have Internet Explorer, which is causing the crash.

In any case, it would make sense to add the proxy server settings to ensure that your application will run on a machine without Internet Explorer installed.

<configuration>  
  <system.net>  
    <defaultProxy enabled="true" useDefaultCredentials="true">  
      <proxy  
        usesystemdefault="true"  
        proxyaddress="http://192.168.1.10:3128"  
        bypassonlocal="true"  
      />  
    </defaultProxy>  
  </system.net>  
</configuration> 

If you want to detect a proxy, there is no way to do it using app.config because that functionality doesn't exist in .NET. Instead, you have to do something along the lines of:

WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
if (proxy.Address.AbsoluteUri != string.Empty)
{
    Console.WriteLine("Proxy URL: " + proxy.Address.AbsoluteUri);
    wc.Proxy = proxy;
}

Reference: C# auto detect proxy settings

0
votes

This is what I ended up doing in the end (basically what NightOwl888 suggested) with GetSystemWebProxy added

var proxy = WebRequest.GetSystemWebProxy();    
_webRequestHandler = new WebRequestHandler { ClientCertificateOptions = ClientCertificateOption.Automatic };
        
_webRequestHandler.Proxy = proxy;
_client = new HttpClient(_webRequestHandler);
        
_client.BaseAddress = new Uri(connectionUrl);
_client.Timeout = new TimeSpan(0,0,0,timeoutSeconds);