1

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?

4
  • is it not compulsory to specify proxy address ? Commented Mar 19, 2018 at 3:19
  • The goal was to automatically detect the proxy. I have no idea what proxy my customers may or may not be using. Commented Mar 19, 2018 at 7:21
  • @rolls, I know it's been a while since you posted the issue, but did you resolve it? Commented Jul 29, 2021 at 10:26
  • yes I did. I'll add answer for what I used. Commented Aug 9, 2021 at 5:23

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

What if I don't know the address? The goal is for my application to use a customer's proxy if they have one set up, if not then don't use it. I don't know in advance what their proxy addresses will be. This is so my application works on people's PC's that have a compulsory proxy in use.
And yes my machine has internet Explorer and a proxy set up.
First rule out some weird machine setup. The fact it crashes is highly suspicious - maybe sign of corrupted machine install/setup, maybe some virus/anti-virus interacting. If you're able to reproduce the crash on more than 1 machine, it would be worth investigating deeper.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.