Default proxy in App.Config cause .NET application to crash with no error

0

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?

c#
.net
proxy
app-config
asked on Stack Overflow Mar 19, 2018 by rolls

1 Answer

0

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

answered on Stack Overflow Mar 19, 2018 by NightOwl888 • edited Mar 19, 2018 by NightOwl888

User contributions licensed under CC BY-SA 3.0