设置带有跟踪的ServicePointManager.ServerCertificateValidationCallback时的TypeInitializationException

时间:2015-05-20 11:11:14

标签: c# .net system.net system.net.sockets system.net.websockets

当我尝试设置

时,我收到了未处理的TypeInitializationException异常
ServicePointManager.ServerCertificateValidationCallback += (a, b, c, d) => true;

当我在app.config中获得以下内容(取自MSDN)时会发生异常。与以下配置和MSDN上的配置的唯一区别是,我已将System.NetSystem.Net.CacheSystem.Net.SocketsSystem.Net.WebSockets的日志记录设置为Off。我只看到所有四个人都关闭的问题。

此配置是否有问题,或者我错过了其他内容?

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

这是异常的堆栈跟踪

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

有一个内部异常,也是TypeInitializationException类型,并且调用堆栈只包含at System.Net.ServicePointManager..cctor(),消息是“System.Net.ComNetOS的类型初始化程序”引发异常。“。

这有类型ConfigurationErrorsException的最终内部异常。消息是“Listener'System.Net'在sharedListeners部分中不存在。”并且堆栈跟踪是

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 个答案:

答案 0 :(得分:1)

System.Net.Http的侦听器名称中有一个空格。

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

这将尝试查找名为"System.Net "的侦听器,但当然您只有一个名为"System.Net"的侦听器。只需删除空间即可,您将获得金色。