TypeInitializationException when setting ServicePointManager.ServerCertificateValidationCallback with tracing off

2.1k Views Asked by At

I'm getting an unhandled TypeInitializationException exception when I try to set

ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => true;

The exception happens when I've got the following (taken from MSDN) in my app.config. The only difference from the config below and that on MSDN is that I've set logging for System.Net, System.Net.Cache, System.Net.Sockets and System.Net.WebSockets to Off. I only see the issue when all four are off.

Is there something wrong with this configuration or am I missing something else?

<system.diagnostics>
  <sources>
    <source name="System.Net" tracemode="includehex" maxdatasize="1024">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Cache">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.Http">
      <listeners>
        <add name="System.Net "/>
      </listeners>
    </source>
    <source name="System.Net.Sockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
    <source name="System.Net.WebSockets">
      <listeners>
        <add name="System.Net"/>
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="System.Net" value="Off"/>
    <add name="System.Net.Cache" value="Off"/>
    <add name="System.Net.Http" value="Verbose"/>
    <add name="System.Net.Sockets" value="Off"/>
    <add name="System.Net.WebSockets" value="Off"/>
  </switches>
  <sharedListeners>
    <add name="System.Net"
         type="System.Diagnostics.TextWriterTraceListener"
         initializeData="network.log"
    />
  </sharedListeners>
  <trace autoflush="true"/>
</system.diagnostics>

Here's the exception's stack track

at System.Net.ServicePointManager.get_ServerCertificateValidationCallback()
at test.Program.Main(String[] args) in c:\Users\Eoin\Documents\Visual Studio 2013\Projects\test\test\Program.cs:line 12
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

There's an inner exception, also of type TypeInitializationException, and the call stack contains only at System.Net.ServicePointManager..cctor() and the message is "The type initializer for 'System.Net.ComNetOS' threw an exception.".

This has a final inner exception of type ConfigurationErrorsException. The message is "Listener 'System.Net ' does not exist in the sharedListeners section." and the stack trace is

at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
at System.Diagnostics.TraceSource.Initialize()
at System.Net.Logging.InitializeLogging()
at System.Net.Logging.get_On()
at System.Net.ComNetOS..cctor()
1

There are 1 best solutions below

0
On BEST ANSWER

There's a space in the name of the listener for System.Net.Http.

<source name="System.Net.Http">
  <listeners>
    <add name="System.Net "/>
  </listeners>
</source>

This will try to find a listener named "System.Net " but of course you only have one named "System.Net". Just remove the space and you're golden.